Ruby on Rails Thursday, August 29, 2013

The documentation is very straight forward and the mentioned example
also cleared all the lines except the one mentioned inside **.

http://ruby-doc.org/core-2.0.0/Module.html#method-i-module_function

Creates module functions for the named methods. These functions may be
called with the module as a receiver, and also become available as
instance methods to classes that mix in the module. Module functions are
copies of the original, and so may be changed independently. The
instance-method versions are made private. *If used with no arguments,
subsequently defined methods become module functions.*


module Mod
def one
"This is one"
end
module_function :one
end
class Cls
include Mod
def call_one
one
end
end
Mod.one #=> "This is one"
c = Cls.new
c.call_one #=> "This is one"
module Mod
def one
"This is the new one"
end
end
Mod.one #=> "This is one"
c.call_one #=> "This is the new one"

Please help me to understand what does mean by "If used with no
arguments, subsequently defined methods become module functions."?

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/5a19164430bc09f5e89b1766a48e5bf3%40ruby-forum.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment