0
0
Rubyprogramming~10 mins

Why single inheritance in Ruby - Test Your Understanding

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

Complete the code to define a class that inherits from another class in Ruby.

Ruby
class ChildClass < [1]
  def greet
    puts 'Hello from child'
  end
end
Drag options to blanks, or click blank then click option'
AParentClass
BChildClass
CObject
DModule
Attempts:
3 left
💡 Hint
Common Mistakes
Using the child class name instead of the parent class name after <.
Trying to inherit from a module using <.
2fill in blank
medium

Complete the code to call the parent class method from the child class in Ruby.

Ruby
class Parent
  def greet
    puts 'Hello from parent'
  end
end

class Child < Parent
  def greet
    [1]
    puts 'Hello from child'
  end
end
Drag options to blanks, or click blank then click option'
Asuper.greet
Bself.greet
CParent.greet
Dsuper
Attempts:
3 left
💡 Hint
Common Mistakes
Calling Parent.greet directly, which is incorrect syntax.
Using self.greet which causes infinite recursion.
3fill in blank
hard

Fix the error in the code to correctly inherit and override a method in Ruby.

Ruby
class Animal
  def speak
    puts 'Animal sound'
  end
end

class Dog [1] Animal
  def speak
    puts 'Bark'
  end
end
Drag options to blanks, or click blank then click option'
A>
B<
C=
Dinherits
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or = instead of < for inheritance.
Writing 'inherits' as a keyword, which Ruby does not support.
4fill in blank
hard

Fill both blanks to create a class that inherits from a parent and calls the parent's method in Ruby.

Ruby
class Vehicle
  def start
    puts 'Vehicle started'
  end
end

class Car [1] Vehicle
  def start
    [2]
    puts 'Car started'
  end
end
Drag options to blanks, or click blank then click option'
A<
Bsuper
Cself
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < for inheritance.
Using self instead of super to call the parent method.
5fill in blank
hard

Fill all three blanks to define a parent class, a child class inheriting it, and call the parent's method in Ruby.

Ruby
class [1]
  def info
    puts 'Parent info'
  end
end

class [2] < [3]
  def info
    super
    puts 'Child info'
  end
end
Drag options to blanks, or click blank then click option'
AParent
BChild
DAnimal
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the parent class in definition and inheritance.
Not calling super to invoke the parent's method.