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