This question already has answers here:
What is Ruby's double-colon `::`?
(12 answers)
Closed 8 years ago.
What does :: mean in Ruby? E.g. Foo::Bar.
From the Pickaxe:
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.
It's called a scope resolution operator. Basically a fancy way of referencing a class within a namespace. ActiveRecord is the namespace and Base is the class.
It accesses constants in a given class or module. E.g. ActiveRecord::Base is the constant Base defined in the module ActiveRecord.
Related
This question already has answers here:
What's the nature of "property" in a Ruby class?
(3 answers)
Closed 7 years ago.
I have a very basic question about Ruby which I can't seem to answer from browsing online:
property :currency, String
What does 'property' above mean? What circumstances should I use it under?
As for many case in Rails, this is (most probably) a carefully disguised method call. Remember that in Ruby, parenthesis around method calls are options, so:
add(3, 4)
add 3, 4
are equivalent. So in your case,
property :currency, String
can actually be written as:
property(:currency, String)
So, a call to a method called "property" which takes two arguments, a Symbol and a Class. The method is most probably defined on a class from a library you are using.
property :currency, String is a method call. When you find a line like that in a class then that means that a method property is called with :currency and String as parameter.
For the special mean of this method, look at the docs
This question already has answers here:
Why are exclamation marks used in Ruby methods?
(12 answers)
Closed 9 years ago.
Is there a default difference between methods with '!' and without, in ruby?
collect v collect!
flatten v flatten!
and so on..
In ruby the main difference is that, the ! methods are selfish, i.e. they apply the changes to the self object. They return nil, when no changes are done, while the non-! methods create new modified object.
In Rails the difference is that, the ! methods are safe versions of non-! methods, that means the ! methods raises an exception when the code encountered an error during execution, while non-! methods just return error state, usually false condition.
This question already has an answer here:
What does class ClassName < ::OtherClassName do in Ruby?
(1 answer)
Closed 8 years ago.
I saw this line of code.
class ClassName < ::TestUnit::Test::Etc
What does it mean when the nested-constant marker follows the inheritance symbol, like so: < ::SuperClass?
Also, is there a technical name for the :: symbol?
:: is the scope resolution operator. It means "look up the following constant name inside this module". If you omit the module, it is assumed to be Object. So, ::Foo is basically the same as Object::Foo except of course that the enclosing module may define its own Object constant, in which case the second form would look up Foo inside that Object instead of the one you expect it to.
Note that :: can also be used as the message sending operator, i.e. the same way as .: foo::bar is the same as foo.bar. This usage is highly discouraged, though.
This question already has an answer here:
naked asterisk as parameter in method definition: def f(*)
(1 answer)
Closed 8 years ago.
In the rails code I came across following method definition def initialize(*)
I understand what def foo(*a) means but can't figure out significance of omitting identifier name after *. How do you access any arguments passed to this method?
Here's my guess.
It works because of second line:
def initialize(*)
super
...
end
So the method receives arbitrary number of arguments and passes all of them to super(as you know, super without arguments means take all arguments from original method).
And then in this case the names for arguments are not required.
This question already has an answer here:
naked asterisk as parameter in method definition: def f(*)
(1 answer)
Closed 10 years ago.
I was poking through the Rails 3 ActiveRecord source code today and found a method where the entire parameter list was a single asterisk.
def save(*)
I couldn't find a good description of what this does (though I have some ideas based on what I know about splat arguments).
What does it do, and why would you use it?
It means it can have any number of arguments (including zero) and it discards all those arguments.