欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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
相关标签: Ruby Gmail

上一篇:

下一篇: