IRB on a diet, for MacRuby / Ruby 1.9
The goal is to have a small and cleaned up version of IRB. Trimmed down to only do the stuff I, and most people I know, actually use.
Trimming down the core code is done mainly by using Ripper, which comes with Ruby 1.9, instead of shipping it’s own parser etc.
There’s still lots to be done, but the ‘basic functionality’ as is now, should not grow too much more. For now my things to-do are .irbrc support, completion, and investigate what else people really really need. After that it’s time to polish.
Important notice
Since on Ruby 1.9 all latest gems are pushed onto the load path by default, installing the DietRB gem would break the existing IRB binary. Therefor, with the DietRB gem is installed, it will hijack the ‘irb’ bin file.
The original IRB will still work when you uninstall the DietRB gem, though.
Extensions
-
irb/ext/colorize.rb, adds support for colorizing the prompt and result. The code was based upon Wirble’s implementation, so your custom Wirble themes should still work.
Configure it with:
-
IRB.formatter.color_scheme = scheme, where scheme can be: :dark_background, :light_background, or :fresh. Defaults to :dark_background.
-
IRB.formatter.colors returns the hash of current token-type to color mappings.
-
-
irb/ext/completion.rb, adds, as the name implies, autocompletion for constants, variables, methods, etc.
-
irb/ext/history.rb, stores/loads the history in and from the history file, which is located at ~/.irb_history.
It provides the following API:
-
Kernel#history(N), or Kernel#h, will show N number of the most recent history entries. Defaults to 50.
-
Kernel#history!(entry_or_range), or Kernel#h!, will execute the specified history entry, or entries if a range is given.
-
Kernel#clear_history! will clear the history and the history file.
-
-
irb/ext/macruby.rb, adds support for Cocoa development by starting an NSRunloop. Loaded by default on MacRuby.
Differences
-
This IRB version specifically targets MacRuby, for now, and allows Cocoa development to be done from the command-line. Dietrb will automatically override the normal runloop to be ran in a thread and start a NSRunLoop on the main thread.
-
Dietrb will try to warn about syntax errors as soon as a line is entered and only reset the buffer to the previous line. This means that you don’t need to loose any previous work:
IRB:
irb(main):001:0> class A irb(main):002:1> def foo irb(main):003:2> } p :ok irb(main):004:1> end SyntaxError: compile error (irb):3: syntax error, unexpected '}' } p :ok ^ (irb):4: syntax error, unexpected $end, expecting kEND from (irb):4 from :0 irb(main):005:0> A.new.foo NameError: uninitialized constant A from (irb):5 from :0
Dietrb:
irb(main):001:0> class A irb(main):002:1> def foo irb(main):003:2> } p :ok SyntaxError: compile error (irb):3: syntax error, unexpected '}' irb(main):004:2> p :ok irb(main):005:2> end irb(main):006:1> end => nil irb(main):007:0> A.new.foo :ok => :ok
Play
Normal usage:
irb(main):001:0> class A
irb(main):002:1> def foo
irb(main):003:2> :ok
irb(main):004:2> end
irb(main):005:1> end
=> nil
irb(main):006:0> irb A.new
irb(#<#<Class:…>::A:…>):001:0> foo
=> :ok
irb(#<#<Class:…>::A:…>):002:0> quit
=> nil
irb(main):007:0> quit
Or on MacRuby, try:
irb(main):001:0> win = NSWindow.alloc.initWithContentRect([200, 300, 250, 100],
irb(main):002:0> styleMask: NSTitledWindowMask|NSResizableWindowMask,
irb(main):003:0> backing: NSBackingStoreBuffered,
irb(main):004:0> defer: false)
=> #<NSWindow:0x20023eb00>
irb(main):005:0> win.orderFrontRegardless
=> #<NSWindow:0x20023eb00>
irb(main):006:0> win.title = 'Hello World'
=> "Hello World"
irb(main):007:0> bye = NSButton.alloc.initWithFrame([10, 10, 80, 80])
=> #<NSButton:0x20027f820>
irb(main):008:0> win.contentView.addSubview(bye)
=> #<NSView:0x200210320>
irb(main):009:0> bye.bezelStyle = NSThickerSquareBezelStyle
=> 4
irb(main):010:0> bye.title = 'Goodbye!'
=> "Goodbye!"
irb(main):011:0> bye.target = NSApp
=> #<NSApplication:0x200257fe0>
irb(main):012:0> bye.action = 'terminate:'
=> "terminate:"
irb(main):013:0> bye.sound = NSSound.soundNamed('Basso')
=> #<NSSound:0x200248b20>