[MacRuby-devel] [MacRuby] #704: Singleton bug with define_method

MacRuby ruby-noreply at macosforge.org
Mon May 10 14:18:33 PDT 2010


#704: Singleton bug with define_method
--------------------------------+-------------------------------------------
 Reporter:  mike@…              |       Owner:  lsansonetti@…        
     Type:  defect              |      Status:  new                  
 Priority:  major               |   Milestone:                       
Component:  MacRuby             |    Keywords:                       
--------------------------------+-------------------------------------------
 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
 }}}

-- 
Ticket URL: <http://www.macruby.org/trac/ticket/704>
MacRuby <http://macruby.org/>



More information about the MacRuby-devel mailing list