From: Gavin Sinclair on
Hi,

Porting a program from 1.8 to 1.9, I hit a stumbling block in the use
of instance_eval.

The following code works in 1.8 but not 1.9. instance_eval in 1.9
does not allow you to use a lambda as a block. The solution is to use
a proc instead. I understand that lambdas are like methods and procs
are like blocks, but what gets me is the incredibly unhelpful error
message.

Anyway, the code (can also see it at http://gist.github.com/479572 )

class InstanceEval
def initialize(x)
@block = x
@context = Object.new
end

def run
code = @block
result = @object.instance_eval(&code)
"* run --> #{result.inspect}"
end
end

p = proc { :p }
l = lambda { :l } # (line 16 -- for the error message below)

puts InstanceEval.new(p).run
puts InstanceEval.new(l).run


The output in 1.8:

* run --> :p
* run --> :l

The output in 1.9:

* run --> :p
code.rb:16:in `block in <main>': wrong number of arguments (1 for 0)
(ArgumentError)
from code.rb:10:in `instance_eval'
from code.rb:10:in `run'
from code.rb:19:in `<main>'

To what method have I passed one argument where zero arguments were expected?

Is the average Ruby programmer supposed to know that instance_eval
can't take a lambda in Ruby 1.9? Perhaps it should say so in the
documentation. (http://bit.ly/96HBeb)

Is there a good reason ( instance_eval &lambda_object ) can't work?

Thanks for any light you can shed on it.

Gavin

From: Stefano Crocco on
On Saturday 17 July 2010, Gavin Sinclair wrote:
> |Hi,
> |
> |Porting a program from 1.8 to 1.9, I hit a stumbling block in the use
> |of instance_eval.
> |
> |The following code works in 1.8 but not 1.9. instance_eval in 1.9
> |does not allow you to use a lambda as a block. The solution is to use
> |a proc instead. I understand that lambdas are like methods and procs
> |are like blocks, but what gets me is the incredibly unhelpful error
> |message.
> |
> |Anyway, the code (can also see it at http://gist.github.com/479572 )
> |
> | class InstanceEval
> | def initialize(x)
> | @block = x
> | @context = Object.new
> | end
> |
> | def run
> | code = @block
> | result = @object.instance_eval(&code)
> | "* run --> #{result.inspect}"
> | end
> | end
> |
> | p = proc { :p }
> | l = lambda { :l } # (line 16 -- for the error message
> |below)
> |
> | puts InstanceEval.new(p).run
> | puts InstanceEval.new(l).run
> |
> |
> |The output in 1.8:
> |
> | * run --> :p
> | * run --> :l
> |
> |The output in 1.9:
> |
> | * run --> :p
> | code.rb:16:in `block in <main>': wrong number of arguments (1 for 0)
> |(ArgumentError)
> | from code.rb:10:in `instance_eval'
> | from code.rb:10:in `run'
> | from code.rb:19:in `<main>'
> |
> |To what method have I passed one argument where zero arguments were
> |expected?
> |
> |Is the average Ruby programmer supposed to know that instance_eval
> |can't take a lambda in Ruby 1.9? Perhaps it should say so in the
> |documentation. (http://bit.ly/96HBeb)
> |
> |Is there a good reason ( instance_eval &lambda_object ) can't work?
> |
> |Thanks for any light you can shed on it.
> |
> |Gavin

Your code runs without errors for me with ruby 1.9.1-p376.

Stefano

From: Gavin Sinclair on
>
> Your code runs without errors for me with ruby 1.9.1-p376.
>
> Stefano

Thanks for testing it. I should have specified my version, of course:

ruby 1.9.2dev (2010-07-02 revision 28524) [i386-cygwin]

Gavin

From: Caleb Clausen on
On 7/17/10, Gavin Sinclair <gsinclair(a)gmail.com> wrote:
>>
>> Your code runs without errors for me with ruby 1.9.1-p376.
>>
>> Stefano
>
> Thanks for testing it. I should have specified my version, of course:
>
> ruby 1.9.2dev (2010-07-02 revision 28524) [i386-cygwin]

It seems like this could be a regression. The error message at the
very least is a bug. I suggest you file a bug report for this.

From: Brian Candler on
Caleb Clausen wrote:
> On 7/17/10, Gavin Sinclair <gsinclair(a)gmail.com> wrote:
>>>
>>> Your code runs without errors for me with ruby 1.9.1-p376.
>>>
>>> Stefano
>>
>> Thanks for testing it. I should have specified my version, of course:
>>
>> ruby 1.9.2dev (2010-07-02 revision 28524) [i386-cygwin]
>
> It seems like this could be a regression.

Seconded. The program works OK for me with ruby 1.9.2dev (2009-07-18
trunk 24186) [i686-linux]
--
Posted via http://www.ruby-forum.com/.