Fwd: Difference between 'def s elf. …' and module_function …



---------- Forwarded message ----------
From: Austin Ziegler < [email protected]>
Date: May 27, 2005 11:15 PM
Subject: Re: Difference between 'def s elf. …' and module_function …
To: ruby-talk ML < [email protected]>

On 5/27/05, Nikolai Weibull
< [email protected]> 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 * [email protected]
              * Alternate: [email protected]

你可能感兴趣的:(Ruby,Gmail)