Please look at this snippet -
class Test
def initialize
@hash = {}
end
def method_missing(m,*a)
if m.to_s =~ /=$/
Test.class_eval do
define_method($.to_sym) do #accessor @hash[$
.to_sym]
end
define_method(m) do |arg| #mutator
@hash[$`.to_sym] = arg.split(",")
end
end
send m, *a
else
raise NoMethodError, “#{m}”
end
end
end
t = Test.new
t.a = “hello”
puts t.a
When I run this from the cmdline I get -
D:\sandbox\nasir>ruby test.rb
test.rb:10:in a': undefined method
to_sym’ for nil:NilClass
(NoMethodError)
from test.rb:25
I was unable to debug this so I started the debugger session but I was
surprised to find that it ran as expected in the debugger session ???-
D:\sandbox\nasir>ruby -r debug test.rb
Debug.rb
Emacs support available.
d:/ruby/lib/ruby/site_ruby/1.8/ubygems.rb:10:require ‘rubygems’
(rdb:1) c
hello
First of all can anyone please explain why it runs with debugger while
fails
with normal interpreter?
Second what is wrong with the snippet?
Thanks