[MacRuby-devel] weird behavior in macirb in forwarder classes

Laurent Sansonetti lsansonetti at apple.com
Sat Sep 5 12:38:02 PDT 2009


Hi Carlo,

On Sep 5, 2009, at 7:27 AM, Carlo Cabanilla wrote:

> Hi,
>
> I'm Carlo, I'm new to MacRuby and I'd like to contribute if  
> possible. I found this weird bug when requiring modules that define  
> classes that extend Forwardable and define delgates:
>
> $ echo "require 'csv'; puts 'good'" | macirb
> require 'csv'; puts 'good'
> NameError: undefined local variable or method `str' for CSV::Row:Class
> 	from 0:in `def_delegator:'
>
> But requiring it from a script works fine:
>
> $ macruby -e "require 'csv'; puts 'good'"
> good
>
> Same behavior for rss/parser:
>
> $ echo "require 'rss/parser'; puts 'good'" | macirb
> require 'rss/parser'; puts 'good'
> NameError: undefined local variable or method `str' for  
> RSS::Parser:Class
> 	from 0:in `def_delegator:'
>
> $ macruby -e "require 'rss/parser'; puts 'good'"
> good
>
>
> I traced it to the def_instance_delegator method in Forwardable:
>
>   def def_instance_delegator(accessor, method, ali = method)
>     str = %Q{
>       def #{ali}(*args, &block)
>         #{accessor}.send(:#{method}, *args, &block)
>       end
>     }
>
>     # If it's not a class or module, it's an instance
>     begin
>       module_eval(str)
>     rescue
>       instance_eval(str)
>     end
>   end
>
>
> I found that if I just hard code the str value as the parameters to  
> module_eval and instance_eval, it works:
>
>   def def_instance_delegator(accessor, method, ali = method)
>     # If it's not a class or module, it's an instance
>     begin
>       module_eval(%Q{
>       def #{ali}(*args, &block)
>         #{accessor}.send(:#{method}, *args, &block)
>       end
>     })
>     rescue
>       instance_eval(%Q{
>       def #{ali}(*args, &block)
>         #{accessor}.send(:#{method}, *args, &block)
>       end
>     })
>     end
>   end
>
> This is kind of hack, so I'm assuming the root cause has to do with  
> how macirb treats variable scope? Is this a known bug? Can I help  
> somehow?

I think this is the same bug that has been reported a few times on  
trac. Currently eval with Binding has a problem with method-scoped  
local variables. This is on the top of our TODO list right now, if you  
want to help you need to discover why MacRuby wrongly compiles local  
variables in this very specific case. The compiler is compiler.cpp.  
Let me know if you find anything interesting or if you have any  
question!

Laurent


More information about the MacRuby-devel mailing list