0

I created some class

class Book
  def perform
    yield
  end
end

Then I want to call the block which will call method1 and method2. However both methods are defined nowhere and I dont want to define them. Instead of this I'd like to call method_missing but I get: undefined local variable or method 'method1' for main:Object (NameError)

book = Book.new
book.perform do
  method1
  method2
end

What Should I do then?

0

1 Answer 1

1

In order to do what you ask I believe you need to redefine method_missing like so:

class Book
  def perform
    yield
  end
end

def method_missing(methodname, *args)
  puts "#{methodname} called"
end

book = Book.new
book.perform do
  method1
  method2
end

#=>
#method1 called
#method2 called
Sign up to request clarification or add additional context in comments.

1 Comment

Great answer, I've tried to overwrite the method_missing inside the class scope and It caused that I didn't work

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.