Bird
Raised Fist0
Rubyprogramming~10 mins

Constants in classes in Ruby - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Constants in classes
Define Class
↓
Set Constant inside Class
↓
Access Constant via Class
↓
Use Constant Value
↓
End
This flow shows how a constant is set inside a class and then accessed using the class name.
Execution Sample
Ruby
class MyClass
  MY_CONST = 10
end

puts MyClass::MY_CONST
Defines a class with a constant and prints the constant value using class scope resolution.
Execution Table
StepActionCode LineState ChangeOutput
1Define class MyClassclass MyClassClass MyClass created
2Set constant MY_CONST to 10MY_CONST = 10MY_CONST set to 10 inside MyClass
3End class definitionendClass MyClass finalized
4Access constant MY_CONST via MyClassputs MyClass::MY_CONSTRetrieve MY_CONST value 1010
💡 Program ends after printing the constant value 10.
Variable Tracker
VariableStartAfter Step 2Final
MyClass::MY_CONSTundefined1010
Key Moments - 2 Insights
Why do we use :: to access the constant?
The :: operator accesses constants or methods inside a class or module. Here, it tells Ruby to look inside MyClass for MY_CONST, as shown in execution_table step 4.
Can we change the value of MY_CONST after setting it?
Constants in Ruby can technically be changed but Ruby warns you. In this example, MY_CONST is set once and used as 10, as shown in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of MY_CONST after step 2?
Anil
B10
Cundefined
Derror
💡 Hint
Check the 'State Change' column in row for step 2 in execution_table.
At which step is the constant MY_CONST accessed to print its value?
AStep 2
BStep 3
CStep 4
DStep 1
💡 Hint
Look for the 'Action' column mentioning 'Access constant' in execution_table.
If we tried to access MY_CONST without MyClass:: prefix, what would happen?
AIt would cause an error
BIt would print 10
CIt would print nil
DIt would print 0
💡 Hint
Constants inside classes need the class name prefix to be accessed, as shown in execution_table step 4.
Concept Snapshot
class MyClass
  MY_CONST = 10  # Define constant inside class
end

Access with MyClass::MY_CONST
Constants are fixed values inside classes
Use :: to access constants inside class scope
Full Transcript
This example shows how to define a constant inside a Ruby class and access it. First, the class MyClass is created. Then, inside it, the constant MY_CONST is set to 10. After the class ends, we access the constant using MyClass::MY_CONST and print its value. Constants inside classes are accessed with the :: operator to specify the class scope. Changing constants is possible but not recommended. The program prints 10 as the output.

Practice

(1/5)
1. What is true about constants in Ruby classes?
easy
A. Constants start with a lowercase letter.
B. Constants can be changed anytime like variables.
C. Constants start with a capital letter and are defined outside methods.
D. Constants must be defined inside methods only.

Solution

  1. Step 1: Understand constant naming rules

    In Ruby, constants always start with a capital letter and are usually defined outside methods within classes or modules.
  2. Step 2: Check where constants are defined

    Constants are not meant to be changed and are not defined inside methods but at the class/module level.
  3. Final Answer:

    Constants start with a capital letter and are defined outside methods. -> Option C
  4. Quick Check:

    Constants = Capital letter + outside methods [OK]
Hint: Constants start uppercase and live outside methods [OK]
Common Mistakes:
  • Thinking constants can be changed like variables
  • Defining constants inside methods
  • Using lowercase letters for constants
2. Which of the following is the correct way to define a constant inside a Ruby class?
easy
A. class MyClass def PI = 3.14 end
B. class MyClass PI = 3.14 end
C. class MyClass pi = 3.14 end
D. class MyClass @PI = 3.14 end

Solution

  1. Step 1: Identify constant syntax

    Constants in Ruby start with a capital letter and are assigned directly inside the class but outside any method.
  2. Step 2: Check each option

    class MyClass PI = 3.14 end correctly assigns PI = 3.14 inside the class. class MyClass pi = 3.14 end uses lowercase (not constant). class MyClass def PI = 3.14 end tries to define a method named PI incorrectly. class MyClass @PI = 3.14 end uses an instance variable (@PI), not a constant.
  3. Final Answer:

    class MyClass PI = 3.14 end -> Option B
  4. Quick Check:

    Constants = Capital letter + direct assignment [OK]
Hint: Constants = Capital letter + direct assignment inside class [OK]
Common Mistakes:
  • Using lowercase names for constants
  • Defining constants inside methods
  • Confusing constants with instance variables
3. What will be the output of this Ruby code?
class Car
  WHEELS = 4
  def wheels
    WHEELS
  end
end

car = Car.new
puts car.wheels
medium
A. 4
B. WHEELS
C. Error: uninitialized constant
D. nil

Solution

  1. Step 1: Understand constant access inside instance method

    The constant WHEELS is defined inside the class Car. Inside instance methods, constants can be accessed directly by their name.
  2. Step 2: Trace the method call

    Calling car.wheels returns the constant WHEELS value, which is 4. Then puts prints 4.
  3. Final Answer:

    4 -> Option A
  4. Quick Check:

    Constant accessed inside method = 4 [OK]
Hint: Constants inside class accessible by name in instance methods [OK]
Common Mistakes:
  • Expecting an error for constant access
  • Thinking constant name prints instead of value
  • Confusing constants with instance variables
4. Identify the error in this Ruby code:
class Book
  TITLE = "Ruby Guide"
  def change_title
    TITLE = "New Title"
  end
end
medium
A. Constants must be defined inside methods.
B. Missing end keyword for class.
C. TITLE should be lowercase.
D. Cannot assign to constant TITLE inside method.

Solution

  1. Step 1: Check constant assignment rules

    Constants in Ruby should not be reassigned once set. Assigning a constant inside a method is not allowed and causes a warning or error.
  2. Step 2: Analyze the code

    The method change_title tries to assign a new value to TITLE constant, which is invalid.
  3. Final Answer:

    Cannot assign to constant TITLE inside method. -> Option D
  4. Quick Check:

    Constants cannot be reassigned inside methods [OK]
Hint: Constants cannot be reassigned inside methods [OK]
Common Mistakes:
  • Trying to change constants inside methods
  • Confusing constants with variables
  • Ignoring Ruby warnings about constant reassignment
5. Given this Ruby code, what will puts Vehicle::TYPE print?
class Vehicle
  TYPE = "Transport"
  class Car
    TYPE = "Car"
  end
end

puts Vehicle::TYPE
hard
A. "Transport"
B. "Car"
C. Error: uninitialized constant
D. nil

Solution

  1. Step 1: Understand constant lookup in nested classes

    Constants are scoped to the class they are defined in. Vehicle::TYPE refers to the constant TYPE inside Vehicle, not inside Vehicle::Car.
  2. Step 2: Check the code output

    Vehicle::TYPE is "Transport". The inner class Car has its own TYPE, but it is not accessed here.
  3. Final Answer:

    "Transport" -> Option A
  4. Quick Check:

    Outer class constant accessed by Vehicle::TYPE = "Transport" [OK]
Hint: Outer class constant accessed by ClassName::CONSTANT [OK]
Common Mistakes:
  • Confusing inner class constant with outer class constant
  • Expecting error for nested constants
  • Using wrong scope operator
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard