What does class ClassName ::OtherClassName do in Ruby?
An runnable example might explain the idea best:
class C
def initialize
puts "At top level"
end
end
module M
class C
def initialize
puts "In module M"
end
end
class P < C
def initialize
super
end
end
class Q < ::C
def initialize
super
end
end
end
M::P.new
M::Q.new
Produces when run:
In module M
At top level
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
What does ClassName :: imply in ruby?
::
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.
ActiveRecord RecordNotFound for other class name
If you look at the error you can see that it occurred in your controller on line 6.
The problem is with Question.tagged(:tag)
. Here you are filtering questions that are tagged with the tag :tag
and you probably haven't created a tag with name :tag
. I believe you wanted to filter questions that are tagged with a tag that is passed in params, so you should use Question.tagged(params[:tag])
.
Why would we put a module inside a class in Ruby?
We could use it when writing ape-like code like this:
class DrugDealer
module Drug
def happy?; true; end
end
def approach(victim)
victim.extend Drug
end
end
o = Object.new
DrugDealer.new.approach(o)
o.happy? # => true
Another example that would be more practical in the real world is to have mixins that are only applied by subclasses.
This is useful when some facets of a thing apply to some subclasses and other facets apply to other subclasses, without there being enough order in the way these aspects apply to make way for a clear class hierarchy (tree). Think multiple inheritance! A simplified example:
class Person
def handshake
:sloppy
end
def mind_contents
:spam
end
module Proper
def handshake
:firm
end
end
module Clever
def mind_contents
:theories
end
end
end
class Professor < Person
include Proper
include Clever
# ...
end
And so on. Kind of nice, when used sensibly. Even super calls and constructors (I didn't define any here though) flow through all the mixins and classes the way I want them to.
Related Topics
Rails 4 User Roles and Permissions
Rails 3 Joins -- Select Only Certain Columns
Weird Rails Error "Permission Denied: Bin/Rails" for Old Rails Apps
Adding a Submit Button Image to a Rails Form
How to Handle Multiple Models in One Rails Form
Ruby Create Recursive Directory Tree
Singleton Method VS. Class Method
Installing Ruby 2.3 on Wsl (Windows Subsystem for Linux)
What Is the Best Wysiwyg for Rails - Ruby on Rails Blog
Using Gets() Gives "No Such File or Directory" Error When I Pass Arguments to My Script
Why Relative Path Doesn't Work in Ruby Require
How to Test for a Redirect with Rspec and Capybara
How to Get All Sundays Between Two Dates in Ruby