Ruby Forum Ruby-core > module_eval change between ruby1.8 & ruby 1.9. Is it intentional ?

Posted by Frederick Cheung (Guest)
on 24.12.2007 12:30
(Received via mailing list)
Hi,

Consider this code

module Foo
end

def do_stuff_to_foo(&block)
   Foo.module_eval &block
end

do_stuff_to_foo {def greeting; 'hello'; end}

If you paste this into irb on 1.9 (r14600, compiled a few minutes ago)
the greeting method is not added to Foo, but to the top level.
On 1.8 it's added to Foo, as I would expect.

Is this change intentional ? I haven't been able to find anything
written about it. I've had a quick scan back through the source and
this isn't a particularly recent change - r11500 exhibits the same
behaviour. I'd be curious to hear the rationale if this is indeed
intentional

Fred
Posted by SASADA Koichi (Guest)
on 24.12.2007 12:39
(Received via mailing list)
Hi,

Frederick Cheung wrote:
> 
> do_stuff_to_foo {def greeting; 'hello'; end}
> 
> If you paste this into irb on 1.9 (r14600, compiled a few minutes ago)
> the greeting method is not added to Foo, but to the top level.
> On 1.8 it's added to Foo, as I would expect.
> 
> Is this change intentional ? I haven't been able to find anything
> written about it. I've had a quick scan back through the source and this
> isn't a particularly recent change - r11500 exhibits the same behaviour.
> I'd be curious to hear the rationale if this is indeed intentional

This is a bug.  If you write a block in method do_stuff_to_foo
directly, it's work fine.  I'll fix it.
Posted by murphy (Guest)
on 24.12.2007 12:42
(Received via mailing list)
Frederick Cheung wrote:
> 
> If you paste this into irb on 1.9 (r14600, compiled a few minutes ago)
> the greeting method is not added to Foo, but to the top level.
> On 1.8 it's added to Foo, as I would expect.
even more strange, this works as expected:

  module Foo
  end

  Foo.module_eval { def greeting; 'hello'; end }

  Foo.instance_methods  # => [:greeting]

[murphy]
Posted by Frederick Cheung (Guest)
on 24.12.2007 12:43
(Received via mailing list)
On 24 Dec 2007, at 11:39, SASADA Koichi wrote:

> directly, it's work fine.  I'll fix it.
>
Cool. Thanks!

Fred
Posted by Kornelius Kalnbach (Guest)
on 01.01.2008 04:58
(Received via mailing list)
SASADA Koichi wrote (2007-12-24):
> This is a bug.  If you write a block in method do_stuff_to_foo
> directly, it's work fine.  I'll fix it.
Any progress? It seems to be the last thing that makes Rails tests fail.

[murphy]
Posted by SASADA Koichi (Guest)
on 03.01.2008 09:30
(Received via mailing list)
Hi,

Kornelius Kalnbach wrote:
> SASADA Koichi wrote (2007-12-24):
>> This is a bug.  If you write a block in method do_stuff_to_foo
>> directly, it's work fine.  I'll fix it.
> Any progress? It seems to be the last thing that makes Rails tests fail.

I wrote [ruby-core:14716] about it.  I can't make it on time of last
release.