Hi there
I need some help understating this behaviour.
I’ve read the docs of define_method but I’m still missing how it exactly
works.
Here’s some code I’ve written trying to use to understand
cat script.rb
#!/bin/ruby
class DefMethod
M_COLLECTION = (‘a’…‘c’)
M_COLLECTION.each do |alpha|
define_method(alpha) {
block_given? ? puts(yield(alpha)) : puts(alpha)
}
end
end
class EvalMethod
DefMethod::M_COLLECTION.each do |alpha|
class_eval <<-ENDEVAL
def #{alpha}
block_given? ? puts(yield(“#{alpha}”)) : puts(“#{alpha}”)
end
ENDEVAL
end
end
defobj = DefMethod.new
evalobj = EvalMethod.new
print “====no block====\n”
defobj.a
evalobj.a
print “====with block====\n”
defobj.a {|x| x + ’ << block given!‘}
evalobj.a {|x| x + ’ << block given!’}
Basicly I’m defining 2 class with 3 method (a,b,c) each in the first
class I use define_method and in the second class_eval.
Now, the output of this script is
ruby script.rb
====no block====
a
a
====with block====
a
a << block given!
So the ‘a’ method is sensible to the block only in the version defined
with class_eval.
After reading the usage of define_method here
I can understand that my first class is basicly wrong but I would
still understand what happens behind the scenes, and how I can manage
to ‘see’ the block using define_method.
Thanks.
Paolo