[MacRuby] #704: Singleton bug with define_method

MacRuby ruby-noreply at macosforge.org
Sat Dec 4 13:42:59 PST 2010


#704: Singleton bug with define_method
--------------------------------+-------------------------------------------
 Reporter:  mike@…              |       Owner:  lsansonetti@…        
     Type:  defect              |      Status:  new                  
 Priority:  major               |   Milestone:                       
Component:  MacRuby             |    Keywords:                       
--------------------------------+-------------------------------------------

Comment(by jhemmelg@…):

 It looks like any I/O causes trouble for define_method.  Here is a
 variation of the above script with a puts statement in the assignment
 method, with different behavior:

 {{{
 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|
         puts "Setting #{name} to #{value}"
         @values[name] = value
       end
       define_method("#{name}") do
         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
 }}}

 And here is the output:

 {{{
 Setting foo to 10.0
 10.0
 Float
 {"Setting foo to 10.0"=>10.0}
 }}}

 The hash key ends up being "Setting foo to 10.0" instead of "foo".

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



More information about the macruby-tickets mailing list