Hi everyone,

I was trying to define instance methods on a class based on an given array of strings. I run into different results using two different types of iteration. I can't understand the resulting behaviour just in a case, the following piece of code will be more meaningful than words:

class Toy
    
end

%w{e1 e2}.each { |m|
    Toy.class_eval do
        define_method m do
            puts m
        end
    end
}

for m in %w{u1 u2}
    Toy.class_eval do
        define_method m do
            puts m
        end
    end
end

p=Toy.new

puts p.e1 # ok
puts p.e2 # ok
puts p.u1 # WTF
puts p.u2 # ok

I tested it with ruby 1.8.7 and with 1.9.2 and I get the same results . Obviously, the third result is the only I don't understand :). It seems that define_method defers the evaluation of "m" object when used with a for.

I'm relatively new to Ruby and  maybe this is the expected behaviour. I would like to understand the difference between the to version, thank you.

P.S. I added this example as a gist, you can read it here https://gist.github.com/868310