macirb tips and tricks

inossidabile edited this page Jan 22, 2013 · 2 revisions

Result of the last expression

Standard irb stores a reference to the result of the previous expression using the reserved _ variable:

irb(main):001:0> NSURL.URLWithString('http://macruby.org/')
=> http://macruby.org/
irb(main):002:0> _.host
=> "macruby.org"
irb(main):003:0> _.gsub /.org/, ''
=> "macruby"
irb(main):004:0> _.transform 'latin-hiragana'
=> "まくるぶい"

The last exception

The IRB implementation that MacRuby uses, DietRB, comes with an additional reserved variable for recording the previous exception:

irb(main):001:0> "testing".inspct
NoMethodError: undefined method `inspct' for "testing":String

irb(main):002:0> e
=> #<NoMethodError: undefined method `inspct' for "testing":String>
irb(main):003:0> puts e.backtrace
/Users/mrada/Developer/AXElements/(irb):1:in `<main>'
/Library/Frameworks/MacRuby.framework/Versions/0.12/usr/bin/macirb:55:in `block'
/Library/Frameworks/MacRuby.framework/Versions/0.12/usr/bin/macirb:9:in `block'
/Library/Frameworks/MacRuby.framework/Versions/0.12/usr/bin/macirb:7:in `<main>'
=> nil

Looking at your history

DietRB also comes with another extension that adds a persistent history. The feature is alluded to in the DietRB README, but you have to see the API documentation here and here to get the juicier details.

irb(main):001:0> h 1
756: exit
irb(main):002:0> h 10
748: e
749: e.backtrace
750: exit
751: "testing".inspect
752: e
753: puts e.backtrace
754: history
755: history 1000
756: exit
757: h 1