Bird
0
0

Find the error in this Ruby code:

medium📝 Debug Q6 of 15
Ruby - Class Methods and Variables
Find the error in this Ruby code:
class Gadget
  VERSION = 1.0
  def update_version
    VERSION = 2.0
  end
end
AVERSION must be defined outside the class
BYou cannot assign a constant inside a method
CMissing 'self' to access constant
DVERSION should be lowercase inside method
Step-by-Step Solution
Solution:
  1. Step 1: Identify constant assignment rules in Ruby

    Constants cannot be assigned inside methods; this causes a syntax error.
  2. Step 2: Locate error in code

    Assigning VERSION = 2.0 inside update_version method is invalid.
  3. Final Answer:

    You cannot assign a constant inside a method -> Option B
  4. Quick Check:

    Constant assignment inside method = error [OK]
Quick Trick: Constants assigned only in class/module body, not methods [OK]
Common Mistakes:
  • Trying to reassign constants inside methods
  • Confusing constants with variables inside methods
  • Assuming 'self' is needed to access constants

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes