Question
In Ruby, what does the double-colon :: operator mean in expressions like Foo::Bar?
I found this definition:
The :: is a unary operator that allows: constants, instance methods and class methods defined within a class or module, to be accessed from anywhere outside the class or module.
What is the practical purpose of this operator, and how does it relate to visibility rules such as private and protected? If :: can access things from outside a class or module, does that mean it bypasses scope and exposes everything?
Short Answer
By the end of this page, you will understand what Ruby's :: operator is mainly used for, especially for namespaces and constants. You will also see that it does not magically bypass private or protected method visibility. In practice, :: is most commonly used to access constants inside modules and classes, such as Math::PI or Admin::User.
Concept
Ruby's double-colon operator :: is primarily used for constant lookup and namespacing.
In Ruby, classes and modules can contain constants, classes, and other modules. The :: operator lets you refer to something defined inside another class or module.
Example:
module Admin
class User
end
end
user_class = Admin::User
Here, User is defined inside the Admin namespace, so Admin::User means “the User constant inside Admin”.
Why this matters
Without namespaces, large Ruby programs would quickly run into naming conflicts.
For example, two different parts of an app might both want a class called User:
module Admin
class User
Mental Model
Think of a module or class as a folder, and constants/classes/modules inside it as files or subfolders.
Admin::Usermeans “open theAdminfolder, then find theUseritem inside it”.Math::PImeans “look inside theMathfolder forPI”.
Now think of private and protected as door locks on actions, not labels on folders.
- A constant is like a named item stored in a folder.
- A method is like an action you can ask an object to perform.
::helps you find the named item.- It does not give you a master key to locked actions.
So :: is more like an addressing tool than a security bypass.
Syntax and Examples
Core syntax
ModuleName::CONSTANT
ModuleName::ClassName
OuterModule::InnerModule::VALUE
::TopLevelConstant
Example 1: Accessing a constant
module AppConfig
VERSION = "1.0.0"
end
puts AppConfig::VERSION
Output:
1.0.0
VERSION is a constant stored inside the AppConfig module.
Example 2: Nested class
module Admin
class User
def
user = .new
puts user.role
Step by Step Execution
Consider this example:
module Store
TAX_RATE = 0.1
class Product
def price_with_tax(price)
price + (price * Store::TAX_RATE)
end
end
end
product = Store::Product.new
puts product.price_with_tax(100)
Step by step
- Ruby defines the module
Store. - Inside
Store, it defines a constantTAX_RATEwith value0.1. - Inside
Store, it defines a classProduct. Store::Product.newmeans:- Find the constant
ProductinsideStore - Treat it as a class
- Find the constant
Real World Use Cases
1. Organizing large applications
Ruby apps often group related classes into modules:
module Payments
class ChargeCustomer
end
class RefundPayment
end
end
Usage:
Payments::ChargeCustomer
Payments::RefundPayment
2. Avoiding naming collisions
Different parts of a system can reuse the same class names safely:
module Api
class Error
end
end
module Billing
class Error
end
end
3. Accessing framework or library constants
Ruby code often uses namespaced library classes:
Real Codebase Usage
In real Ruby codebases, :: is most often used as a namespace separator.
Common patterns
Namespacing service objects
module Payments
class CreateInvoice
def call
# ...
end
end
end
Used as:
Payments::CreateInvoice.new.call
Grouping errors
module Auth
class Error < StandardError; end
class InvalidTokenError < Error; end
end
Used as:
raise Auth::InvalidTokenError,
Common Mistakes
1. Thinking :: is mainly for private method access
This is the most common misunderstanding.
Incorrect idea
class Demo
private
def secret
"hidden"
end
end
d = Demo.new
# Expecting :: to bypass privacy
d::secret
Do not think of :: as a privacy bypass. Ruby visibility rules still matter, and this is not how :: is typically used.
2. Confusing constants with variables
Constants start with a capital letter in Ruby.
Broken example
module App
version = "1.0"
end
puts App::version
This does not work because version is a local variable, not a constant.
Correct version
=
puts
Comparisons
| Concept | What it accesses | Typical use | Affects privacy? |
|---|---|---|---|
:: | Constants, nested classes/modules, namespaces | Math::PI, Admin::User | No |
. | Methods on an object | user.name, array.length | Yes, method visibility matters |
::Name | Top-level constant lookup | ::String, ::Array | No |
:: vs .
Cheat Sheet
Quick reference
Use :: for
- Accessing constants
- Accessing nested classes or modules
- Referring to top-level constants
Common syntax
ModuleName::CONSTANT
ModuleName::ClassName
Outer::Inner::VALUE
::String
Examples
Math::PI
Admin::User
Net::HTTP
::Array
Key rules
::is mainly for constant lookup- Class names and module names are constants in Ruby
privateandprotectedcontrol methods, not constants
FAQ
Is :: the same as . in Ruby?
No. . is for calling methods. :: is mainly for constant and namespace lookup.
Can :: access private methods in Ruby?
In normal Ruby code, you should not think of :: as a way to bypass private methods. Method visibility rules still apply.
Why is Admin::User written with ::?
Because User is a constant defined inside the Admin module. :: tells Ruby to look inside that namespace.
What does ::String mean in Ruby?
It means the top-level String constant, which is useful if a local namespace also defines String.
Are classes and modules constants in Ruby?
Yes. That is why you can access them with ::.
Does private hide constants in Ruby?
Mini Project
Description
Create a small Ruby program that models a namespaced application configuration and service class. This project demonstrates how :: is used to access nested classes and constants in a realistic way, without confusing it with method visibility.
Goal
Build a namespaced Ruby program that uses :: to access constants and nested classes correctly.
Requirements
- Create a module named
AppConfigwith at least one constant. - Create another module named
Reportscontaining a class calledGenerator. - Make
GeneratoruseAppConfig::to read configuration. - Instantiate the class using its full namespaced path.
- Include one private helper method inside the class to show that
::does not replace method visibility.
Keep learning
Related questions
Calling an Overridden Monkey-Patched Method in Ruby
Learn how to call the original method when monkey patching in Ruby, including alias_method patterns, examples, pitfalls, and practical usage.
Difference Between require and include in Ruby
Learn the difference between require and include in Ruby, when to load a file, and when to mix module methods into a class.
Fixing Ruby Gem Native Extension Errors: mkmf.rb Can't Find Header Files for Ruby
Learn why Ruby gem installs fail with missing ruby.h, how native extensions work, and how to fix header file errors on Linux servers.