Issue #6151 has been updated by Thomas Sawyer.


That's fun, go to report a bug in Ruby and hit a bug in Ruby's Bug tracker.

Try again, this time with no RD crap.

Given:

      #
      class BlockParser < BasicObject
        def initialize(data={}, &block)
          @data = data
          instance_eval(&block)
        end

        def method_missing(name, *args, &block)
          if block
            @data[name] = {}
            BlockParser.new(@data[name], &block)
          else
            case args.size
            when 0
              @data[name]
            when 1
              @data[name] = args.first
            else
              @data[name] = args
            end
          end
        end
      end

      data = {}
      blk = lambda do
        name 'tommy'
      end

      BlockParser.new(data, &blk)
      ArgumentError: wrong number of arguments (1 for 0)

But it works if we use:

      blk = Proc.new do
        name 'tommy'
      end

Since BlockParser is using instance_eval, I don't see why this should be an error. Indeed, how can I depend on it if my API simply accepts a block --I don't know if it's a Proc or a lambda.


----------------------------------------
Bug #6151: BasicObject instance_eval of lambda causes errors
https://bugs.ruby-lang.org/issues/6151#change-24618

Author: Thomas Sawyer
Status: Open
Priority: Normal
Assignee: 
Category: core
Target version: 
ruby -v: ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]


=begin

Perhaps there is a reason, but it sure seems like a bug to me. Given this simple class:

      #
      class BlockParser < BasicObject
        def initialize(data={}, &block)
          @data = data
          instance_eval(&block)
        end

        def method_missing(name, *args, &block)
          if block
            @data[name] = {}
            BlockParser.new(@data[name], &block)
          else
            case args.size
            when 0
              @data[name]
            when 1
              @data[name] = args.first
            else
              @data[name] = args
            end
          end
        end
      end

Then we can do:

      data = {}
      BlockParser.new(data) do
        name 'Tommy'
      end
      data #=> {:name=>'Tommy'}

But,

      data = {}
      blk  = lambda do
        name 'Tommy'
      end
      BlockParser.new(data, &blk)
      ArgumentError: wrong number of arguments (1 for 0)
	from (irb):44:in `block in irb_binding'
	from (irb):16:in `instance_eval'
	from (irb):16:in `initialize'
	from (irb):46:in `new'
	from (irb):46
	from /home/trans/.rbfu/rubies/1.9.3-p0/bin/irb:12:in `<main>'

If I use `Proc.new` instead of `lambda` it works. But since I am using `#instance_eval` to evaluate the Proc, that shouldn't matter.

Note the reported line number of the error corresponds to `name 'Tommy'`.
=end



-- 
http://bugs.ruby-lang.org/