I got tired of typing
require 'pp' and the like each time I loaded up my console, that I was joyed to discover I could configure it: using the
.irbrc file (win32 explanation below).
Here is mine:
require 'irb/completion' require 'map_by_method' require 'what_methods' require 'pp' IRB.conf[:AUTO_INDENT]=true
Here is what each line does:
Line 1 – Auto-completion in IRB
You can discover what methods can be called on an object. Double-TAB is your “exploration” combo.
irb(main):001:0> "hello".to_<double TAB here> .to_a .to_f .to_i .to_s .to_str .to_sym irb(main):003:0> a = "hello" => "test" irb(main):004:0> a.t<double TAB here> a.taint a.to_f a.to_str a.tr! a.type a.tainted? a.to_i a.to_sym a.tr_s a.to_a a.to_s a.tr a.tr_s!
Not understanding this? Watch the video
Line 2 – Map by Method
When in the console its great to be able to explore and manipulate data as quickly as possible. The Map by Method (previously called Map by Pluralisation) is wizardly for quick-to-type array manipulations.
gem install map_by_method
Line 3 – MethodFinder/Object.what?
Ever asked: “if I have an object, what method can I call on it to get that result?”
See if this suits your console cravings:
> 3.45.what? 3 3.45.truncate == 3 3.45.to_i == 3 3.45.prec_i == 3 3.45.floor == 3 3.45.to_int == 3 3.45.round == 3 => ["truncate", "to_i", "prec_i", "floor", "to_int", "round"] > 3.45.what? 4 3.45.ceil == 4 => ["ceil"] > 3.55.what? 4 3.55.ceil == 4 3.55.round == 4 => ["ceil", "round"]
Just what you need in the console.
I’ve gemified a library found on _why’s blog by Nikolas Coukouma.
gem install what_methods
Line 4 – pretty print
pp = pretty print – wondeful in the console for exploring objects that are visually large.
It prints, and its pretty.
Line 5 – auto-tabbing turned on
Auto-tabbing is the console’s way of trying to help your code look pretty by indenting your blocks, etc.
It’s only primitive, but better than a kick in the teeth.
> [1,2,3].each do |a| * puts a > end 1 2 3 => [1, 2, 3]
.irbrc for Win32
Create a file called anything you like (e.g. “_irbrc” or “irb.rc”) and place it anywhere you like (say C:\Documents and Settings\
C:\Documents and Settings\
More ideas at the bottom of this.
Any other useful libraries to include in irb/console?
 You can run
irb from anywhere, but to use
console for a specific rails app, you need to point to the console script for that app, e.g
ruby script/console from the rails app root folder.
- The explicit Ruby metaclass you know you always wanted When you define a “static” or “class” method on a...
- map_by_method now works with ActiveRecord associations I was always annoyed that map_by_method was broken for ActiveRecord...
- Magic Wiggly Lines => GuessMethod, by Chris Shea If you ever make time to code just for pleasure,...
- Magic Multi-Connections: A “facility in Rails to talk to more than one database at a time” At this point in time thereâ€™s no facility in Rails...
- [ANN] Generating new gems for graceful goodliness I don’t like you . You don’t share code....