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.
Related
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
class Library
attr_accessor :games
def initialize(games)
#games = games
end
end
How comes there is a colon infront of games?
Whenever I research what the : means I usually find articles where people say its a symbol, then give a very ambiguous definition of what a symbol is.
To keep it simple: symbols are nothing more than a name for a constant. The value is irrelevant, but only symbols with the same name share the same value:
Symbol objects represent names [...] inside the Ruby interpreter. They are generated using the :name and :"string" literals syntax, and by the various to_sym methods.
See Ruby docs on Symbol for more details.
The usual use cases for symbols include:
keys for hashes (hsh[:foo] 0 42; hsh[:foo] #=> 42)
method or variable names, since those types of objects are not first-class citizens of Ruby (there is a Method class representing a method, but that's something different)
Please note, that symbols won't be garbage collected, unlike strings. That means, you should avoid code like
key = :"oh-my-#{bar}" # like string interpolation
when bar is build from user (attacker) generated input.
In the case of attr_accessor, the parameter (:games in your case), creates an instance variable with the same name (#games) plus a setter and getter method (let l = Library.new, then the setter l.games=(val) and the getter l.games become available).
Instances of Symbol class. However it's mentioned and explained in all tutorials or beginner guides I've seen so far.
The colon indicates the variable is a symbol. Symbols are Strings, with one important difference, Symbols are immutable. Mutable objects can be changed after assignment while immutable objects can only be overwritten.
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 answers here:
Closed 10 years ago.
Possible Duplicate:
What is Ruby’s double-colon (::) all about?
pardon for my laziness. I tried to guess. I am not sure what the double '::Logger' does in this case?
https://github.com/wycats/rack-offline/blob/master/lib/rack/offline.rb#L25
it seems like it is initializing the object and assign it on a variable that is not in its scope? line 25 is wrapped by {begin/end} block and gets assigned to #logger
Just like a / in a path defines nested directories, :: accesses nested classes.
And also similarly to a leading /, a leading :: means to start at the the very top of the tree. It starts searching for constants at the global scope.
# Bar declared in global scope
class Bar
end
# Foo declared in global scope
class Foo
# A different class named Bar declared in the scope of Foo, not global
class Bar
end
Bar #=> refers to Foo::Bar, that is class Bar declared within Foo
::Bar #=> refers to outer global scope class named Bar
end
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.