[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Difference between 'def self.…' and module_function …

Nikolai Weibull

5/27/2005 3:03:00 PM

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:/...!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}


5 Answers

Austin Ziegler

5/27/2005 3:16:00 PM

0

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> endModule A defines only A.a. Module B defines B#a and B.a. B.a is a copyof B#a at the time of the call to module_function and B#a is madeprivate (according to the documentation).module A def self.a puts "#{self.inspect}.a" endendmodule B def a puts "#{self.inspect}.a" end module_function :aendA.methods(false) # -> [ "a" ]B.methods(false) # -> [ "a" ]B.instance_methods(false) # -> []B.private_instance_methods(false) # -> ["a"]A.a # -> A.aB.a # -> B.ao = Object.newo.extend(B)o.send(:b) # -> #<Object:0x2b3fe38>.aDoes that make it clearer?-austin-- Austin Ziegler * halostatue@gmail.com * Alternate: austin@halostatue.ca

Dave Burt

5/28/2005 2:22:00 AM

0

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


Nikolai Weibull

5/28/2005 4:24:00 PM

0

Austin Ziegler wrote:

â?®

> Does that make it clearer?

Yes. Thanks,
nikolai

--
Nikolai Weibull: now available free of charge at http:/...!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}


Nikolai Weibull

5/28/2005 4:33:00 PM

0

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:/...!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}


Dave Burt

5/29/2005 2:24:00 AM

0

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