Here’s my current code:
class << self
NAMES.each do |item_name|
item = nil
define_method "#{item_name}" do
item = find_by_name(item_name)
end
define_method "#{item_name}_id" do
item.id
end
end
end
I am getting a error when I try to call Item.balloon_id, for example, because it say’s can’t call nil on object. What’s the correct code?
Its hard to tell because you don’t say what class we are “in”. I’m guessing we’re in
Item, but I am a little unclear why you have
item = nilitem = nilI presume is obfuscating a method by the same name for your instance variable @item.Another way to do it would be to call the method you defined above.
E.G