[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

module_function semantics

William Webber

9/5/2003 3:57:00 AM

Hi all!

I'm trying to understand some code in the stdlib (drb/drb.rb, to be
particular).

Is there any difference between:

module Foo

module_function
def bar()
"baz"
end

end

and

module Foo

def Foo.bar()
"baz"
end
end

?

Am I right in guessing that the former gives both a module and a mixin
function of the same name, whereas the latter gives on a module function?
Or are they in fact identical?

William
1 Answer

ts

9/5/2003 8:32:00 AM

0

>>>>> "W" == William Webber <william@williamwebber.com> writes:

W> module Foo

W> module_function

module_function make a private method and a public singleton method.

W> def bar()
W> "baz"
W> end

W> end

For example


svg% cat b.rb
#!/usr/bin/ruby
module A
def a
puts "a"
end
module_function :a

def self.b
puts "b"
end
end

A.a
A.b

include A
a
b
svg%

svg% b.rb
a
b
a
./b.rb:18: undefined local variable or method `b'' for main:Object (NameError)
svg%


Guy Decoux