This works:
class MyAwesomeClass
variable_of_doom = ["things", "stuff", "chunky_bacon"]
variable_of_doom.each do |name|
define_method("#{name}") do
puts name
end
end
end
puts (MyAwesomeClass.instance_methods - Object.instance_methods).inspect
# => ["things", "stuff", "chunky_bacon"]
这不是,但这是我需要的:
variable_of_doom = ["things", "stuff", "chunky_bacon"]
class MyAwesomeClass
variable_of_doom.each do |name|
define_method("#{name}") do
puts name
end
end
end
puts (MyAwesomeClass.instance_methods - Object.instance_methods).inspect
# => NameError: undefined local variable or method ‘variable_of_doom’ for MyAwesomeClass:Class
# => method <class:MyAwesomeClass> in untitled 4 at line 9
# => method <main> in untitled 4 at line 7