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



---------- 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值