0
0
Rubyprogramming~10 mins

Class instance variables as alternative 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 class instance variable in Ruby.

Ruby
class MyClass
  [1] = 10
end
Drag options to blanks, or click blank then click option'
A@@value
B@@count
C@value
D@count
Attempts:
3 left
💡 Hint
Common Mistakes
Using '@@' instead of '@' for class instance variables.
Defining variables inside methods instead of the class body.
2fill in blank
medium

Complete the code to access a class instance variable inside a class method.

Ruby
class MyClass
  @count = 5

  def self.get_count
    [1]
  end
end
Drag options to blanks, or click blank then click option'
A@count
B@@count
Cself.@count
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using '@@count' which refers to class variables, not class instance variables.
Trying to use 'self.@count' which is invalid syntax.
3fill in blank
hard

Fix the error in the code to correctly initialize and access a class instance variable.

Ruby
class MyClass
  [1] = 0

  def self.get_value
    @value
  end
end
Drag options to blanks, or click blank then click option'
A@value
B@@value
Cself.@value
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing class instance variables inside instance methods.
Using '@@' for class instance variables.
4fill in blank
hard

Fill both blanks to create a class instance variable and a class method that returns it.

Ruby
class MyClass
  [1] = 100

  def self.get_value
    [2]
  end
end
Drag options to blanks, or click blank then click option'
A@value
B@@value
D@@count
Attempts:
3 left
💡 Hint
Common Mistakes
Using '@@' instead of '@' for class instance variables.
Trying to access class instance variables with wrong syntax.
5fill in blank
hard

Fill all three blanks to define a class instance variable, a class method to set it, and a class method to get it.

Ruby
class MyClass
  [1] = nil

  def self.set_value(val)
    [2] = val
  end

  def self.get_value
    [3]
  end
end
Drag options to blanks, or click blank then click option'
A@value
D@@value
Attempts:
3 left
💡 Hint
Common Mistakes
Using '@@value' instead of '@value'.
Trying to set class instance variables inside instance methods.