What does Ruby's "class NewClass < ::OtherClass" syntax mean? [duplicate] - ruby

This question already has answers here:
Ruby's double colon (::) operator usage differences
(2 answers)
Closed 6 years ago.
I know that to use class inheritance in Ruby, the following syntax is used:
class MyNewClass < SomeClass
...
end
I also know that nesting in namespaces is identified using :::
class SomeNameSpace::MyNewClass < SomeNameOtherSpace::SomeClass
...
end
However, what does the following syntax mean?
class SomeNameSpace::MyNewClass < ::SomeClass
...
end
I expect that ::SomeClass (so without anything before the ::)is a shorthand for something, but what exactly does it mean?

::SomeClass means SomeClass class from top namespace. :: is specially used to refer the top namespace from deep inside other modules.

Related

Use 'puts' with block accepting method over multiple lines [duplicate]

This question already has answers here:
Ruby Print Inject Do Syntax
(3 answers)
Closed 7 years ago.
Let's say I have the following (working) pseudo-code:
puts results.fields.inject('') { |string, key|
lengths[key] = calculate_length(key)
string << format(key)
}
Following the ruby-style-guide I should
Omit parentheses for 'keyword methods', i.e. puts, (which would create }) or end) anyway)
Use do...end for multi-line blocks
However, when replacing {...} with do...end it raises
undefined method `' for :title:Symbol (NoMethodError)
Therefore, is it possible to refactor this code without violenting the guideline?
There is a difference in precedence between {} and do end
If you use do end the block is associated with the puts statement, whereas the braces are associated with the results.field.inject('')
If you want to use do end then you have to remove the ambiguity of association by parentheses. It's one of those "gotchas" where the guidelines are recognized as guidelines, not absolute rules.
See also this answer...
In Ruby, why is a method invocation not able to be treated as a unit when "do" and "end" is used?

The ampersand operator with parameters [duplicate]

This question already has answers here:
Can you supply arguments to the map(&:method) syntax in Ruby?
(9 answers)
Closed 8 years ago.
I wonder, isn't it possible to call a method using & operator with parameters?
items.each &:my_proc # ok
items.each &:my_proc(123, "456") # ops!
No, it's not possible. Use full form.
items.each{|i| i.my_proc(123, '456')}
Look at the source of Symbol#to_proc for the "why".
You can use a bit of trickery and acheive something similar:
class Symbol
def [](*args)
proc{|obj| obj.send(self, *args) }
end
end
[123.456, 234.567].map(&:round[2])
#=> [123.46, 234.57]
I highly discourage the use in production code though, since gems etc may rely on Symbol#[]. This is just a fun thing to play around with ;-)

What does ClassName < :: imply in ruby? [duplicate]

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.

What does method_name(*) mean? [duplicate]

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.

What does :: (double colon) mean in Ruby? [duplicate]

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.

Resources