What Does ::Myclass Ruby Scope Operator Do

What does ::MyClass Ruby scope operator do?

This explicitly refers to the MyClass in the global scope. If there is a MyClass in the global scope, but also a MyClass inside of SomeModule, referring to MyClass from inside of SomeModule will refer to MyClass inside of the module, not the global MyClass. Saying ::MyClass explicitly refers to the MyClass in the global scope.

class MyClass
def self.something
puts "Global MyClass"
end
end

module SomeModule
class MyClass
def self.something
puts "SomeModule::MyClass"
end
end

print "From the module: "
MyClass.something

print "Explicitly using global scope: "
::MyClass.something
end

print "From the global scope: "
MyClass.something

print "Explicitly using module scope: "
SomeModule::MyClass.something

What is the :: sign/operator before the class name in ruby?

It is to resolve against the global scope instead of the local.

class A
def self.global?
true
end
end

module B

class A
def self.global?
false
end
end

def self.a
puts A.global?
puts ::A.global?

end
end

B::a

prints

false
true

Ruby scopes: Diference between MyClass.new and ::MyClass.new

Imagine the following code:

class A
def a
puts 'TOPMOST'
end
end

module B
class A
def a
puts 'NESTED'
end
end
def self.topmost
::A.new.a
end
def self.nested
A.new.a
end
end

B.topmost will print "TOPMOST", and B.nested will print "NESTED".

So, ::A means not “from ruby core”, but rather “from no module.”

What is Ruby's double-colon `::`?

:: is basically a namespace resolution operator. It allows you to access items in modules, or class-level items in classes. For example, say you had this setup:

module SomeModule
module InnerModule
class MyClass
CONSTANT = 4
end
end
end

You could access CONSTANT from outside the module as SomeModule::InnerModule::MyClass::CONSTANT.

It doesn't affect instance methods defined on a class, since you access those with a different syntax (the dot .).

Relevant note: If you want to go back to the top-level namespace, do this: ::SomeModule – Benjamin Oakes

When is scope resolution necessary in Ruby (ActiveRecord)

You need to use the scope resolution operator so Ruby will not look for MyModel inside the MyModel namespace.

def custom_validation
if ::MyModel.where(some_field: 1).count > 0
errors.add(:some_field, "foo")
end
end

rspec: describe MyClass::Something do

You are confused by Ruby syntax, not RSpec syntax. MyClass is a module, and Something is a class or module inside the MyClass module. The :: is the scope resolution operator to tell Ruby which Something you are looking for.

module Foo
class Bar
def say_hello
puts "hello"
end
end
end

foo = Foo::Bar.new
foo.say_hello
#prints "hello"

See http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html for more on modules.

Ruby: what does :: prefix do?

The :: is the scope resolution operator. What it does is determines what scope a module can be found under. For example:

module Music
module Record
# perhaps a copy of Abbey Road by The Beatles?
end

module EightTrack
# like Gloria Gaynor, they will survive!
end
end

module Record
# for adding an item to the database
end

To access Music::Record from outside of Music you would use Music::Record.

To reference Music::Record from Music::EightTrack you could simply use Record because it's defined in the same scope (that of Music).

However, to access the Record module responsible for interfacing with your database from Music::EightTrack you can't just use Record because Ruby thinks you want Music::Record. That's when you would use the scope resolution operator as a prefix, specifying the global/main scope: ::Record.

Ruby on Rails: How to use scope named 'open'?

In Rails 3, scope and class method are basically the same thing.

I think you are calling the instance method instead of the class method.

class Foo
scope :open, where(:closed => false)

def open
#instance_method
end
end
# how to call them
Foo.open # scope/class method
Foo.new.open # instance_method

When is scope resolution necessary in Ruby (ActiveRecord)

You need to use the scope resolution operator so Ruby will not look for MyModel inside the MyModel namespace.

def custom_validation
if ::MyModel.where(some_field: 1).count > 0
errors.add(:some_field, "foo")
end
end


Related Topics



Leave a reply



Submit