Ticket #704 (new defect)
Singleton bug with define_method
| Reported by: | mike@… | Owned by: | lsansonetti@… |
|---|---|---|---|
| Priority: | major | Milestone: | MacRuby 1.0 |
| Component: | MacRuby | Keywords: | |
| Cc: |
Description
I'm trying to dynamically add methods to my class in MacRuby. I'm getting a crash in my program. I haven't been able to cut that down to a smaller example yet, but I did find this problem when I tried.
The following program should print this output (and does from Ruby 1.9):
10.0
Float
{"foo"=>10.0}
With MacRuby, it prints
NilClass
{"foo"=>10.0}
If you uncomment the print statement, it starts working. Here's the code
class SingletonTest
attr_reader :values
def initialize
@values = {}
end
def addMethod(name)
singleton = class << self
self
end
singleton.instance_exec(name) do |name|
define_method("#{name}=") do |value|
@values[name] = value
end
define_method("#{name}") do
# puts "returning value"
return @values[name]
end
end
end
end
singleton_test = SingletonTest.new()
singleton_test.addMethod('foo')
singleton_test.foo = 10.0
value = singleton_test.foo
puts value
puts value.class
puts singleton_test.values
Change History
Note: See
TracTickets for help on using
tickets.

