├── .gitignore ├── Makefile ├── README.md ├── irb ├── awesome_print_loader.rb ├── cucumber_console.rb ├── gem_loader.rb ├── pry_loader.rb ├── rails_colors.rb ├── rails_commands.rb ├── rspec_console.rb └── ruby18.rb ├── irbrc └── pryrc /.gitignore: -------------------------------------------------------------------------------- 1 | irb/custom.rb 2 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | GEMS = pry pry-doc awesome_print 2 | GEMS_NODEPS = commands rspec-console cucumber-console 3 | TARGETS = $(HOME)/.pryrc $(HOME)/.irbrc 4 | SHELL = /usr/bin/env bash 5 | CWD = $(shell pwd) 6 | RVM=$(shell ls -d {~/.,/usr/local/}rvm 2>/dev/null | head -1) 7 | RBENV=$(shell rbenv root) 8 | 9 | ifneq ($(HOME)/.irb, $(shell pwd)) 10 | $(error Please rename this directory to ~/.irb) 11 | endif 12 | 13 | ifneq ($(RVM),) 14 | RUBIES=$(shell ls ${RVM}/rubies | grep -v '^default$$') 15 | else ifneq ($(RBENV),) 16 | RUBIES=$(shell rbenv versions | sed 's/\*//g' | awk '{print $$1}' | grep -v system) 17 | else 18 | $(error Please use rvm or rbenv) 19 | endif 20 | 21 | define check_file 22 | @if [[ -e $1 && "$(OVERWRITE)" != "1" ]]; then \ 23 | echo "make install won't overwrite $1"; \ 24 | echo "1) remove it yourself or 2) use 'make install OVERWRITE=1'"; \ 25 | false \ 26 | else true; \ 27 | fi 28 | endef 29 | 30 | bold=`tput bold` 31 | normal=`tput sgr0` 32 | 33 | install: $(TARGETS) 34 | @echo -e "gems: ${bold}${GEMS} ${GEMS_NODEPS}${normal}\n" 35 | @for RUBY in ${RUBIES}; do \ 36 | if [ -n "${RVM}" ]; then \ 37 | echo "[rvm] Installing gems for ${bold}$$RUBY@global${normal}..."; \ 38 | rvm $$RUBY@global do gem install ${GEMS} --no-ri --no-rdoc; \ 39 | rvm $$RUBY@global do gem install ${GEMS_NODEPS} --ignore-dependencies --no-ri --no-rdoc; \ 40 | rvm $$RUBY@global do gem cleanup; \ 41 | else \ 42 | echo "[rbenv] Installing gems for ${bold}$$RUBY${normal}..."; \ 43 | export RBENV_VERSION=$$RUBY; \ 44 | rbenv rehash; \ 45 | gem install ${GEMS} --install-dir ${RBENV}/versions/$$RUBY/lib/ruby/gems/irb-config --no-ri --no-rdoc; \ 46 | gem install ${GEMS_NODEPS} --install-dir ${RBENV}/versions/$$RUBY/lib/ruby/gems/irb-config --ignore-dependencies --no-ri --no-rdoc; \ 47 | GEM_HOME=${RBENV}/versions/$$RUBY/lib/ruby/gems/irb-config gem cleanup; \ 48 | fi; \ 49 | echo; \ 50 | done 51 | @echo "${bold}Enjoy !${normal}" 52 | 53 | $(HOME)/.%: % 54 | $(call check_file,$@) 55 | ln -fs $(PWD)/$< $@ 56 | 57 | update: 58 | git pull 59 | +make install 60 | 61 | .PHONY: install update 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Installation 2 | ------------ 3 | 4 | Prerequisites: rvm or rbenv. 5 | 6 | To install, run 7 | 8 | git clone git://github.com/nviennot/irb-config.git ~/.irb 9 | cd ~/.irb 10 | make install 11 | 12 | `make install` installs all the required gems for all your installed rubies. 13 | When installing a new ruby, please `make update` in the ~/.irb directory. 14 | 15 | Updates 16 | -------- 17 | 18 | To update the repository and all the gems, run 19 | 20 | make update 21 | 22 | What is it ? 23 | ------------ 24 | 25 | ### Watch the screencast 26 | 27 | [![Watch the screencast!](https://s3.amazonaws.com/velvetpulse/screencasts/irb-config-screencast.jpg)](http://velvetpulse.com/2012/11/19/improve-your-ruby-workflow-by-integrating-vim-tmux-pry/) 28 | 29 | It packages: 30 | - [Pry](https://github.com/pry/pry) 31 | - [Pry Doc](https://github.com/pry/pry-doc) 32 | - [Awesome Print](https://github.com/michaeldv/awesome_print) 33 | - [Commands](https://github.com/rails/commands) 34 | - [RSpec Console](https://github.com/nviennot/rspec-console) 35 | - [Cucumber Console](https://github.com/nviennot/cucumber-console) 36 | 37 | This way you can switch back and forth from the development environment and the 38 | test environment, which is what the rspec/cucumber commands do. 39 | 40 | How to use 41 | ---------- 42 | 43 | * All the goodies are automatically loaded into your rails console. 44 | * Use the `rspec` command pretty much like the usual one. 45 | * Use the `cucumber` command pretty much like the usual one. 46 | * Use the `rake`, `test`, `generate`, `destroy`, `update` commands as usual. 47 | * Type `help` to see the Pry help. 48 | 49 | Notes 50 | ----- 51 | 52 | * All the gems from your global gemset can be loaded bypassing Bundler. 53 | * The RSpec/Cucumber context run with your test environment, including your test 54 | database settings. Furthermore, whenever you run the rspec command, all your 55 | classes are reloaded with `reload!`. 56 | 57 | Vim Integration 58 | ---------------- 59 | 60 | With the [Screen](https://github.com/ervandew/screen) plugin, you can 61 | communicate with screen/tmux to send some commands. I find these one 62 | particularly useful: 63 | 64 | command -nargs=? -complete=shellcmd W :w | :call ScreenShellSend("load '".@%."';") 65 | map c :ScreenShellVertical bundle exec rails c 66 | map r :w :call ScreenShellSend("rspec ".@% . ':' . line('.')) 67 | map e :w :call ScreenShellSend("cucumber --format=pretty ".@% . ':' . line('.')) 68 | map b :w :call ScreenShellSend("break ".@% . ':' . line('.')) 69 | 70 | This is setup in my [Vim configuration](https://github.com/nviennot/vim-config/). 71 | 72 | Assuming you have a tmux session with vim and the rails console: 73 | * `:W` saves and reloads the current file in the console. 74 | * `,c` opens a tmux pane with a rails console. 75 | * `,r` saves the file and run the rspec test corresponding to the cursor line. 76 | * `,e` saves the file and run the cucumber test corresponding to the cursor line. 77 | * `,b` puts a break point on the current line 78 | 79 | License 80 | -------- 81 | 82 | irb config is released under the MIT license. 83 | -------------------------------------------------------------------------------- /irb/awesome_print_loader.rb: -------------------------------------------------------------------------------- 1 | module IRB 2 | module AwesomePrintLoader 3 | def self.setup 4 | return unless IRB.try_require 'awesome_print' 5 | ::Pry.config.print = proc do |output, value| 6 | ::Pry::Helpers::BaseHelpers.stagger_output("=> #{value.ai}", output) 7 | end 8 | end 9 | setup 10 | end 11 | end 12 | 13 | -------------------------------------------------------------------------------- /irb/cucumber_console.rb: -------------------------------------------------------------------------------- 1 | module IRB 2 | module CucumberConsole 3 | def self.setup 4 | return unless IRB.try_require 'cucumber-console' 5 | ::CucumberConsole::Pry.setup 6 | end 7 | setup 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /irb/gem_loader.rb: -------------------------------------------------------------------------------- 1 | module IRB 2 | module GemLoader 3 | def self.irb_gemsetpath 4 | # For rbenv, we load the gems from our special directory 5 | path = `rbenv which ruby 2> /dev/null`.chop.split('/')[0..-3].join('/') rescue nil 6 | return path + "/lib/ruby/gems/irb-config" if path.to_s.size > 0 7 | 8 | # On RVM, We make everything in the global gemset available, bypassing Bundler 9 | (ENV["_ORIGINAL_GEM_PATH"] || ENV["GEM_PATH"] || `gem env path`.chop).split(':').grep(/ruby.*@global/).first rescue nil 10 | end 11 | 12 | def self.setup 13 | irb_gemset = irb_gemsetpath 14 | Dir["#{irb_gemset}/gems/*"].each { |path| $LOAD_PATH << "#{path}/lib" } if irb_gemset 15 | end 16 | setup 17 | end 18 | 19 | def self.try_require(file) 20 | begin 21 | require file 22 | true 23 | rescue LoadError 24 | warn "=> Unable to load #{file}" 25 | false 26 | end 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /irb/pry_loader.rb: -------------------------------------------------------------------------------- 1 | module IRB 2 | module Pry 3 | def self.setup 4 | return unless IRB.try_require 'pry' 5 | 6 | load_pry_plugins 7 | trap_winchange 8 | 9 | ::Pry.prompt = [proc { |obj, nest_level, pry| get_prompt(obj, nest_level, pry, ">") }, 10 | proc { |obj, nest_level, pry| get_prompt(obj, nest_level, pry, "|") }] 11 | @@home = Dir.home 12 | end 13 | 14 | def self.get_prompt(obj, nest_level, pry, suffix) 15 | obj_str = obj.inspect.gsub(/\n/, '') 16 | max_length = 20 17 | if obj_str.size > max_length+3 18 | obj_str = "#{obj_str[0, max_length/2]}...#{obj_str[-max_length/2..-1]}" 19 | end 20 | "#{self.pwd} (#{obj_str})#{":#{nest_level}" unless nest_level.zero?} #{suffix} " 21 | end 22 | 23 | def self.linux? 24 | require 'rbconfig' 25 | /linux/i === RbConfig::CONFIG["host_os"] 26 | end 27 | 28 | def self.load_pry_plugins 29 | IRB.try_require 'pry-doc' 30 | end 31 | 32 | def self.trap_winchange 33 | return unless linux? # mac crashes too often 34 | 35 | # thanks @rking 36 | trap :WINCH do 37 | size = `stty size`.split(/\s+/).map(&:to_i) 38 | Readline.set_screen_size(*size) rescue nil 39 | Readline.refresh_line 40 | end 41 | end 42 | 43 | def self.pwd 44 | Dir.pwd.gsub(/^#{@@home}/, '~') 45 | end 46 | end 47 | 48 | class TopLevel 49 | def to_s 50 | defined?(Rails) ? Rails.env : "main" 51 | end 52 | alias inspect to_s 53 | Object.__send__(:include, Rails::ConsoleMethods) if defined?(Rails::ConsoleMethods) 54 | end 55 | end 56 | 57 | IRB::Pry.setup 58 | -------------------------------------------------------------------------------- /irb/rails_colors.rb: -------------------------------------------------------------------------------- 1 | module IRB 2 | module RailsLogger 3 | # Cyan messages for Rails logger 4 | def self.setup 5 | require 'logger' 6 | Rails.logger = Logger.new(STDERR) 7 | Rails.logger.level = Logger::INFO 8 | Rails.logger.formatter = proc do |severity, datetime, progname, msg| 9 | "\033[36m#{msg}\033[0m\n" 10 | end 11 | rescue 12 | end 13 | setup if defined? ::Rails 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /irb/rails_commands.rb: -------------------------------------------------------------------------------- 1 | module IRB 2 | module RailsCommands 3 | def self.setup 4 | return unless IRB.try_require 'commands' 5 | 6 | ::Pry::CommandSet.new do 7 | [:rake, :generate, :destroy, :update].each do |cmd| 8 | create_command cmd.to_s, "Works pretty much like the regular #{cmd} command" do 9 | group "Rails Commands" 10 | define_method(:process) do |*args| 11 | # Yes, the join is sad. 12 | ::Rails::Commands::Commander.new.__send__(cmd, args.join(' ')) 13 | end 14 | end 15 | end 16 | end.tap { |cmd| ::Pry::Commands.import cmd } 17 | end 18 | setup if defined? ::Rails::ConsoleMethods 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /irb/rspec_console.rb: -------------------------------------------------------------------------------- 1 | module IRB 2 | module RSpecConsole 3 | def self.setup 4 | return unless IRB.try_require 'rspec-console' 5 | ::RSpecConsole::Pry.setup 6 | end 7 | setup 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /irb/ruby18.rb: -------------------------------------------------------------------------------- 1 | class Dir 2 | def self.home 3 | File.expand_path('~') 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /irbrc: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require '~/.irb/irb/ruby18.rb' if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("1.9") 3 | require '~/.irb/irb/gem_loader' 4 | require '~/.irb/irb/pry_loader' 5 | require '~/.irb/irb/custom' if File.exists?("#{Dir.home}/.irb/irb/custom.rb") 6 | 7 | if defined? ::Pry 8 | IRB::TopLevel.new.pry 9 | exit 10 | end 11 | -------------------------------------------------------------------------------- /pryrc: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require '~/.irb/irb/ruby18.rb' if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("1.9") 3 | require '~/.irb/irb/gem_loader' 4 | require '~/.irb/irb/pry_loader' 5 | require '~/.irb/irb/custom' if File.exists?("#{Dir.home}/.irb/irb/custom.rb") 6 | require '~/.irb/irb/awesome_print_loader' 7 | require '~/.irb/irb/rspec_console' 8 | require '~/.irb/irb/cucumber_console' 9 | require '~/.irb/irb/rails_colors' 10 | require '~/.irb/irb/rails_commands' 11 | --------------------------------------------------------------------------------