Skip to content

Instantly share code, notes, and snippets.

@alloy
Created June 18, 2009 09:03
Show Gist options
  • Save alloy/131803 to your computer and use it in GitHub Desktop.
Save alloy/131803 to your computer and use it in GitHub Desktop.
require "irb"
IRB.init_config(__FILE__)
# Muchos hackos! I probably initialize IRB in the wrong way...
def IRB.CurrentContext
o = Object.new
def o.last_value
''
end
o
end
module Kernel
private
def break!(&block)
raise ArgumentError, "Need to give at least an empty block for context to work." unless block
# Probably catching the wrong object or something else, because typing either `exit' or `quit'
# should not exit the application but rather continue where we left off.
catch(:IRB_EXIT) { IRB::Irb.new(IRB::WorkSpace.new(block.binding)).eval_input }
end
end
class Foo
def initialize(val)
@val = val
x = 10
foo = :foo
break! {}
end
def i_is_a_method!
"Original value was `#{@val}'"
end
end
p Foo.new('Hello macabre world!')
# Ruby 1.8:
#
# % ruby irb_test.rb
# irb(#<Foo:0x3c258>):001:0> foo
# => :foo
# irb(#<Foo:0x3c258>):002:0> x
# => 10
# irb(#<Foo:0x3c258>):003:0> i_is_a_method!
# => "Original value was `Hello macabre world!'"
# irb(#<Foo:0x3c258>):004:0>
# Ruby 1.9:
#
# % ruby19 irb_test.rb
# irb(#<Foo:0x4290a0>):001:0> foo
# => :foo
# irb(#<Foo:0x4290a0>):002:0> x
# => 10
# irb(#<Foo:0x4290a0>):003:0> i_is_a_method!
# => "Original value was `Hello macabre world!'"
# irb(#<Foo:0x4290a0>):004:0>
# MacRuby 0.5 (experimental)
#
# % ./miniruby -I lib irb_test.rb
# irb: warn: can't alias exit from irb_exit.
# irb(#<Proc:0x800393940>):001:0> foo
# => :foo
# irb(#<Proc:0x800393940>):002:0> x
# => 10
# irb(#<Proc:0x800393940>):003:0> i_is_a_method!
# NoMethodError: undefined method `i_is_a_method!' for #<Proc:0x800393940>
# from 0:in `eval:'
# from 0:in `evaluate:'
# from 0:in `evaluate:'
# from 0:in `signal_status:'
# from 0:in `class_eval:'
# from 0:in `each_top_level_statement'
# from 0:in `eval_input'
# from 0:in `break!'
# from 0:in `initialize:'
# from 0:in `__new__:'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment