I thought I new the difference between writing
module A
def self.a
⋮
end
end
and
module A
def a
⋮
end
module_function :a
end
But I just realized I don’t. Would someone mind explaining it to me?
Thanks,
nikolai
···
--
Nikolai Weibull: now available free of charge at http://bitwi.se/!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}
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
···
On 5/27/05, Nikolai Weibull <mailing-lists.ruby-talk@rawuncut.elitemail.org> wrote:
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca
Hi "Nikolai Weibull" <mailing-lists.ruby-talk@rawuncut.elitemail.org>,
I think there are two differences:
#NW> I thought I new [sic.] the difference between writing
module A; def self.a; nil; end; end
=> nil
#NW> and
module B; def a; nil; end; module_function :a; end
=> B
#NW> But I just realized I don't. Would someone mind explaining it to me?
A.private_instance_methods
=>
B.private_instance_methods
=> ["a"]
The first, insignificant, difference is that module_function returns the
module, so the result of the latter module definition is the module itself.
The second difference is that the latter case leaves a private instance
method behind.
Cheers,
Dave
Dave Burt wrote:
Hi "Nikolai Weibull" <mailing-lists.ruby-talk@rawuncut.elitemail.org>,
Don’t worry, I’m definitely Nikolai Weibull; the quotes are unnecessary.
#NW> I thought I new [sic.] the difference between writing
I’d prefer to not be sic’d, thanks. In email quotations it’s obvious
that I made the error, not you. But as long as we’re at it, “sic” is
written “sic”. It’s not an abbreviation, so there’s no dot. “Sic” is a
latin word meaning “thus”.
The first, insignificant, difference is that module_function returns the
module, so the result of the latter module definition is the module itself.
The second difference is that the latter case leaves a private instance
method behind.
OK, thanks. I guess I don’t really see the application of this, but I
guess I will once I’ll actually need to :-),
nikolai
···
--
Nikolai Weibull: now available free of charge at http://bitwi.se/\!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}
Nikolai wrote:
I'd prefer to not be sic'd, thanks. In email quotations it's obvious
that I made the error, not you. But as long as we're at it, "sic" is
written "sic". It's not an abbreviation, so there's no dot. "Sic" is a
latin word meaning "thus".
Sorry for siccing you, I don't know what I was thinking. Thank you for the
correction and etymology.
The second difference is that the latter case leaves a private instance
method behind.
OK, thanks. I guess I don't really see the application of this, but I
guess I will once I'll actually need to :-),
nikolai
I think the main purpose is simply style.
There is another way to do this, as well, good for when you have many
functions:
module A
class << self
def a
end
end
end
I never prefer module_function.
Cheers,
Dave