Guest User

Untitled

a guest
Apr 25th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. require "irb"
  2.  
  3. IRB.init_config(__FILE__)
  4.  
  5. # Muchos hackos! I probably initialize IRB in the wrong way...
  6. def IRB.CurrentContext
  7. o = Object.new
  8. def o.last_value
  9. ''
  10. end
  11. o
  12. end
  13.  
  14. module Kernel
  15. private
  16.  
  17. def break!(&block)
  18. raise ArgumentError, "Need to give at least an empty block for context to work." unless block
  19. # Probably catching the wrong object or something else, because typing either `exit' or `quit'
  20. # should not exit the application but rather continue where we left off.
  21. catch(:IRB_EXIT) { IRB::Irb.new(IRB::WorkSpace.new(block.binding)).eval_input }
  22. end
  23. end
  24.  
  25. class Foo
  26. def initialize(val)
  27. @val = val
  28. x = 10
  29. foo = :foo
  30.  
  31. break! {}
  32. end
  33.  
  34. def i_is_a_method!
  35. "Original value was `#{@val}'"
  36. end
  37. end
  38.  
  39. p Foo.new('Hello macabre world!')
  40.  
  41. # Ruby 1.8:
  42. #
  43. # % ruby irb_test.rb
  44. # irb(#<Foo:0x3c258>):001:0> foo
  45. # => :foo
  46. # irb(#<Foo:0x3c258>):002:0> x
  47. # => 10
  48. # irb(#<Foo:0x3c258>):003:0> i_is_a_method!
  49. # => "Original value was `Hello macabre world!'"
  50. # irb(#<Foo:0x3c258>):004:0>
  51.  
  52. # Ruby 1.9:
  53. #
  54. # % ruby19 irb_test.rb
  55. # irb(#<Foo:0x4290a0>):001:0> foo
  56. # => :foo
  57. # irb(#<Foo:0x4290a0>):002:0> x
  58. # => 10
  59. # irb(#<Foo:0x4290a0>):003:0> i_is_a_method!
  60. # => "Original value was `Hello macabre world!'"
  61. # irb(#<Foo:0x4290a0>):004:0>
  62.  
  63. # MacRuby 0.5 (experimental)
  64. #
  65. # % ./miniruby -I lib irb_test.rb
  66. # irb: warn: can't alias exit from irb_exit.
  67. # irb(#<Proc:0x800393940>):001:0> foo
  68. # => :foo
  69. # irb(#<Proc:0x800393940>):002:0> x
  70. # => 10
  71. # irb(#<Proc:0x800393940>):003:0> i_is_a_method!
  72. # NoMethodError: undefined method `i_is_a_method!' for #<Proc:0x800393940>
  73. # from 0:in `eval:'
  74. # from 0:in `evaluate:'
  75. # from 0:in `evaluate:'
  76. # from 0:in `signal_status:'
  77. # from 0:in `class_eval:'
  78. # from 0:in `each_top_level_statement'
  79. # from 0:in `eval_input'
  80. # from 0:in `break!'
  81. # from 0:in `initialize:'
  82. # from 0:in `__new__:'
Add Comment
Please, Sign In to add comment