Fwd: Difference between 'def s elf. …' and module_function … RubyGmail
程序员文章站
2024-01-09 13:16:40
...
---------- Forwarded message ----------
From: Austin Ziegler <halostatue@gmail.com>
Date: May 27, 2005 11:15 PM
Subject: Re: Difference between 'def s elf. …' and module_function …
To: ruby-talk ML <ruby-talk@ruby-lang.org>
On 5/27/05, Nikolai Weibull
< mailing-lists.ruby-talk@rawuncut.elitemail.org> wrote:
> I thought I new the difference between writing
> module A
> def self.a
> ⋮
> end
> end
>
> and
>
module B
> def a
> ⋮
> end
>
> module_function :a
> end
Module A defines only A.a. Module B defines B#a and B.a. B.a is a copy
of B#a at the time of the call to module_function and B#a is made
private (according to the documentation).
module A
def self.a
puts "#{self.inspect}.a"
end
end
module B
def a
puts "#{self.inspect}.a"
end
module_function :a
end
A.methods(false) # -> [ "a" ]
B.methods(false) # -> [ "a" ]
B.instance_methods(false) # -> []
B.private_instance_methods(false) # -> ["a"]
A.a # -> A.a
B.a # -> B.a
o = Object.new
o.extend(B)
o.send(:b) # -> #<Object:0x2b3fe38>.a
Does that make it clearer?
-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca