Module.foobar vs Module::foobar ?
-
module Foo def self.bar puts 'Hello World' end end
Is there any difference to calling
Foo.bar
vsFoo::bar
? -
They are sometimes synonymous. The . form is preferred if you are calling a method Module should reasonably be expected to perform, especially one that changes Module. The :: suggests that you are calling a service provided by Module.
-
A "service"? That does that mean in Ruby world?
-
@thomthom said:
A "service"? That does that mean in Ruby world?
It puts "Hello, World!" to the console. It doubles the size of a ComponentInstance along an axis. It returns the value of pi to a ridiculous number of decimal places.
-
@martinrinehart said:
It puts "Hello, World!" to the console. It doubles the size of a ComponentInstance along an axis. It returns the value of pi to a ridiculous number of decimal places.
You've just described a 'method'.
-
The
::
is the scope operator. Isn't the.
(dot) an operator meaning "send this message to the receiver."?Here's something:
http://stackoverflow.com/questions/43134/is-there-a-difference-between-and-when-calling-class-methods-in-ruby -
I still don't understand what the difference between calling a method with
::
vs.
is... -
@thomthom said:
I still don't understand what the difference between calling a method with
::
vs.
is...That because of Tim Toady. When there are so many ways to do the same thing it makes learning the "thing" (in this case Ruby) extremely complicated. In Python we use the "DOT" to call modules, classes, methods, and functions. Also we use the identifier without parenthesis to get a reference to a class, method, or function. (no symbols needed!). Our built-ins have just enough methods and NOT too many for good reason. And we never have more than one method that does exactly the same thing ...like the following (Note: "S" is the Selection instance)
- S#at(idx) and S#[idx]
- S#count() and S#length() and S#nitems()
- S#single_object? and S#length|nitems|count == 1
- S#empty? and S#length|nitems|count == 0
- S#each{...} and for x in selection...end
- S#include?(obj) and S#contains?(obj)
- S#first() and begin S#[0] rescue nil end
- the unexpected rules for add and remove
Just image for a moment if instead of just 'Yield!' signs we also had "Look Out!", "Be Careful!", "Submit!", "Bend Over!", "FIFO", "VIP's first", "Take it Like a man!". Well at least then folks would have an excuse for not reading and obeying the signs!
-
@thomthom said:
I still don't understand what the difference between calling a method with
::
vs.
is...From "Programming Ruby : The Pragmatic Programmer's Guide"
@unknownuser said:
(http://phrogz.net/ProgrammingRuby/language.html#therubylanguage) 'Invoking a Method'":385b8bf0]A method is called by passing its name to a receiver. If no receiver is specified, self is assumed.
The receiver checks for the method definition in its own class and then sequentially in its ancestor classes. The instance methods of included modules act as if they were in anonymous superclasses of the class that includes them. If the method is not found, Ruby invokes the method method_missing in the receiver. The default behavior defined in Kernel::method_missing is to report an error and terminate the program.
When a receiver is explicitly specified in a method invocation, it may be separated from the method name using either a period "." or two colons "::". The only difference between these two forms occurs if the method name starts with an uppercase letter. In this case, Ruby will assume that a receiver::Thing method call is actually an attempt to access a constant called Thing in the receiver unless the method invocation has a parameter list between parentheses.
-
Thanks Dan. That put my mind at rest.
-
Module.foobar is an alias for Module::foobar, which is itself an alias for Ruby::is_fubar?, which incidentally, always returns the TrueClass instance, for which jessejames's above statement is an alias.
-
Advertisement