0

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?

1 Answer 1

2

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 = nil

item = nil I 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

define_method "#{item_name}_id" do
  self.send(item_name).id
end
Sign up to request clarification or add additional context in comments.

1 Comment

What does the "send" method do?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.