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?
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.