Referencing a method in a module

C

Charles Roper

Say I have the following:

module Foo

def bar
puts "Baz"
end

end

If I want to write about (in some documentation, or a bug report) the
bar method of the Foo module, what's the best way of referring to it? E.g.,

I have found a bug in Foo::bar
I have found a bug in Foo.bar
I have found a bug in the bar method of the Foo module

Or some other way?

Many thanks!

Charles
 
F

Farrel Lifson

Foo#bar is usually used for instance methods, Foo.bar for class
methods. In this case I think Foo#bar is correct.

Farrel
 
C

Charles Roper

Foo#bar is usually used for instance methods, Foo.bar for class
methods. In this case I think Foo#bar is correct.

Ah, yes, I think I've seen that. And so Foo::Bar would be for a class
within a module?

Many thanks, Farrel.

Charles
 
R

Rick DeNatale

Ah, yes, I think I've seen that. And so Foo::Bar would be for a class within
a module?

More generally

Foo::Bar

refers to the constant named Bar within the namespace named Foo.

Foo could be either a Module or a Class

The value of Bar could be a Module, a Class, or actually any value

module Foo
class Bar
Baz = 42
end
module ClassMethods
end
end

Foo is a module here,

Foo::Bar is a class

Foo::ClassMethods is a module

Foo:Bar::Baz is an integer.

--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 
C

Charles Roper

More generally

=C2=A0Foo::Bar

refers to the constant named Bar within the namespace named Foo.

Rick, many thanks, your explanation is most illuminating.

Cheers,
Charles
 

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

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top