Difference between 'def self.…' and module_function …

N

Nikolai Weibull

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
 
D

Dave Burt

Hi "Nikolai Weibull" <[email protected]>,

I think there are two differences:

#NW> I thought I new [sic.] the difference between writing=> nil
#NW> and=> 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
 
N

Nikolai Weibull

Dave said:
Hi "Nikolai Weibull" <[email protected]>,

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
 
D

Dave Burt

Nikolai said:
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.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top