F
Frederick Cheung
Consider the following:
module Foo
end
def do_stuff_to_foo(&block)
Foo.module_eval &block
end
In ruby 1.8 you can do something like this
do_stuff_to_foo {def greeting; 'hello'; end}
Foo.instance_methods #=> ['hello']
In ruby 1.9 however, no method is added to Foo, instead the method is
added to whoever called do_stuff_to_foo:
do_stuff_to_foo {def greeting; 'hello'; end}
Foo.instance_methods #=> []
greeting #=> 'hello'
Of course if you do
Foo.module_eval {def greeting; 'hello'; end}
then the method is added to Foo on both 1.8 & 1.9
I had a look through http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9
but I didn't see anything that seemed relevant.
Is this intended? Is there a way to write the do_stuff_to_foo method
in ruby 1.9 ?
Thanks,
Fred
module Foo
end
def do_stuff_to_foo(&block)
Foo.module_eval &block
end
In ruby 1.8 you can do something like this
do_stuff_to_foo {def greeting; 'hello'; end}
Foo.instance_methods #=> ['hello']
In ruby 1.9 however, no method is added to Foo, instead the method is
added to whoever called do_stuff_to_foo:
do_stuff_to_foo {def greeting; 'hello'; end}
Foo.instance_methods #=> []
greeting #=> 'hello'
Of course if you do
Foo.module_eval {def greeting; 'hello'; end}
then the method is added to Foo on both 1.8 & 1.9
I had a look through http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9
but I didn't see anything that seemed relevant.
Is this intended? Is there a way to write the do_stuff_to_foo method
in ruby 1.9 ?
Thanks,
Fred