Bird
0
0

In Ruby, where are constants typically defined inside a class?

easy📝 Conceptual Q1 of 15
Ruby - Class Methods and Variables
In Ruby, where are constants typically defined inside a class?
AInside local blocks like loops
BDirectly inside the class body, outside any method
CInside instance methods
DOnly in modules, not classes
Step-by-Step Solution
Solution:
  1. Step 1: Understand constant placement in Ruby classes

    Constants are defined directly inside the class body, not inside methods or blocks.
  2. Step 2: Identify correct placement

    Since constants belong to the class itself, they must be declared outside instance methods.
  3. Final Answer:

    Directly inside the class body, outside any method -> Option B
  4. Quick Check:

    Constant placement = inside class body [OK]
Quick Trick: Constants go outside methods but inside class body [OK]
Common Mistakes:
  • Defining constants inside methods
  • Trying to define constants inside loops or blocks
  • Thinking constants only belong in modules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes