0
0
Rubyprogramming~10 mins

Constants in classes in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a constant named GREETING inside the class.

Ruby
class Welcome
  [1] = "Hello!"
end
Drag options to blanks, or click blank then click option'
AGreeting
BgREETING
Cgreeting
DGREETING
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase letters for constant names.
Using variable names instead of constants.
2fill in blank
medium

Complete the code to access the constant PI inside the class MathConstants.

Ruby
class MathConstants
  PI = 3.14

  def self.get_pi
    [1]
  end
end
Drag options to blanks, or click blank then click option'
API
BMathConstants.pi
Cself.PI
Dpi
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase letters to access constants.
Trying to call constants like methods.
3fill in blank
hard

Fix the error in accessing the constant MAX_SPEED from outside the class Vehicle.

Ruby
class Vehicle
  MAX_SPEED = 120
end

speed = [1]
Drag options to blanks, or click blank then click option'
AVehicle->MAX_SPEED
BVehicle.MAX_SPEED
CVehicle::MAX_SPEED
DVehicle.MAXspeed
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot notation instead of double colon.
Using incorrect operator like '->'.
4fill in blank
hard

Fill both blanks to define and access a constant SPEED_LIMIT inside the class Car.

Ruby
class Car
  [1] = 80

  def speed_limit
    [2]
  end
end
Drag options to blanks, or click blank then click option'
ASPEED_LIMIT
Bspeed_limit
Dself.SPEED_LIMIT
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase names for constants.
Trying to access constants with dot notation inside instance methods.
5fill in blank
hard

Fill all three blanks to create a constant NAME, access it inside a method, and access it from outside the class Person.

Ruby
class Person
  [1] = "Alice"

  def name
    [2]
  end
end

puts [3]
Drag options to blanks, or click blank then click option'
ANAME
CPerson::NAME
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase names for constants.
Trying to access constants like instance variables.
Using dot notation outside the class.