Despite numerous attempts I am unable to create a method using the define_method() and supplying a method.
If I understand the documentation for the Module class that can be found here http://www.ruby-doc.org/core-1.9.3/Module.html I should be able to do either of the following:
define_method(symbol, method) → new_method
define_method(symbol) { block } → proc
I am able to use define_method(symbol) { block } however what I receive seems to be a method (not a proc as outlined on in the doc I linked to):
class M
define_method(:hello) { puts "Hello World!"}
end
M.new.hello
My two concerns here are:
1. Doing the above I don’t seem to be receiving a proc despite the doc clearly stating that’s what I would get.
2. I have no clue how to supply a method for “define_method(symbol, method) → new_method”, I tried googling to no avail, not sure how to use this form of define_method.
If anyone could please shed any light on this that would be greatly appreciated! 🙂 Many thanks!
define_methoddoes indeed return aProcor aMethoddepending on usage.In the first case, a
Procis returned:Running the above on the console outputs:
The second case returns an
UnboundMethod, which is a type ofMethod:The above outputs
This is an extremely contrived example, and defining a method to pass to
define_methodisn’t really useful in this case, nor can I think of a case where it would be.