Created
August 7, 2012 21:37
-
-
Save calebhearth/3289590 to your computer and use it in GitHub Desktop.
The Dangers of Ruby 'for' and metaprogramming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
THINGS = %w{ foo bar baz } | |
class EachThing | |
THINGS.each do |thing| | |
define_method(thing) { thing } | |
end | |
end | |
class ForThing | |
for thing in THINGS do | |
define_method(thing) { thing } | |
end | |
end | |
puts "============EACH===========" | |
puts "foo: #{ EachThing.new.foo }" | |
puts "bar: #{ EachThing.new.bar }" | |
puts "baz: #{ EachThing.new.baz }" | |
puts "============FOR============" | |
puts "foo: #{ ForThing.new.foo }" | |
puts "bar: #{ ForThing.new.bar }" | |
puts "baz: #{ ForThing.new.baz }" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
============EACH=========== | |
foo: foo | |
bar: bar | |
baz: baz | |
============FOR============ | |
foo: baz | |
bar: baz | |
baz: baz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment