Back
Spot the difference
Other·Calvin Correli·Nov 5, 2008· 1 minutes

This does what you’d expect:

%w(msgtype state chstat chstatmsg qpstat qpstatmsg merchant merchantemail cardtype).each do |attr|
  define_method(attr) do
    params[attr]
  end
end

This does not:

for attr in %w(msgtype state chstat chstatmsg qpstat qpstatmsg merchant merchantemail cardtype)
  define_method(attr) do
    params[attr]
  end
end

The latter version uses the last value for attr (in this case, cardtype) for each method it defines.

I thought for x in y was just syntactilcal sugar. Apparently not. You live, you learn :)