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

Carlo Cabanilla carlo.cabanilla at gmail.com
Sat Sep 5 07:27:08 PDT 2009


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?

-- 


.Carlo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20090905/0d11d4be/attachment.html>


More information about the MacRuby-devel mailing list