0
0
Rubyprogramming~10 mins

Subclass with < operator 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 subclass of Array.

Ruby
class MyArray < [1]
end
Drag options to blanks, or click blank then click option'
AString
BHash
CArray
DObject
Attempts:
3 left
💡 Hint
Common Mistakes
Using Hash or String instead of Array as the superclass.
Forgetting the < operator.
2fill in blank
medium

Complete the code to override the < operator to compare lengths.

Ruby
class MyArray < Array
  def <(other)
    self.length [1] other.length
  end
end
Drag options to blanks, or click blank then click option'
A<
B>
C==
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using == or > instead of < for comparison.
Comparing the arrays directly instead of their lengths.
3fill in blank
hard

Fix the error in the method to correctly compare array lengths.

Ruby
class MyArray < Array
  def <(other)
    self.size [1] other.size
  end
end
Drag options to blanks, or click blank then click option'
A==
B<=
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using == or > instead of <.
Using length and size inconsistently.
4fill in blank
hard

Fill both blanks to complete the subclass and define the < operator comparing sums.

Ruby
class MyArray [1] Array
  def <(other)
    self.sum [2] other.sum
  end
end
Drag options to blanks, or click blank then click option'
A<
B>
C<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or >= instead of < or <=.
Forgetting to inherit properly.
5fill in blank
hard

Fill all three blanks to define a subclass with < operator comparing max values.

Ruby
class MyArray [1] Array
  def <(other)
    self.max [2] other.max && self.min [3] other.min
  end
end
Drag options to blanks, or click blank then click option'
A<
B<=
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up comparison operators.
Using > or < inconsistently.