i’m trying to create some getters via metaprogramming,but for some reason it does not seems to work. I’m doing this :
RESOURCES = %w(wood stone gold)
class << self
RESOURCES.each do |resource|
define_method "#{resource}" do
resource
end
end
end
Inside a rails application, but i get a wood undefined method, upon executing. Any ideas why ?
(That code is inside a Rails “class City < ActiveRecord::Base” class)
You are defining a class method, not an instance method.
Watch what happens when you try:
Assuming you want it to be an instance method: