Spot the difference

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 :)

0 comments

There are no comments yet. Be the first one to leave a comment!

Leave a comment