0
0
Rubyprogramming~10 mins

Accessing parent methods 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 call the parent method from the child class.

Ruby
class Parent
  def greet
    puts "Hello from Parent"
  end
end

class Child < Parent
  def greet
    [1]
  end
end

Child.new.greet
Drag options to blanks, or click blank then click option'
Asuper
Bself.greet
CParent.greet
Dgreet
Attempts:
3 left
💡 Hint
Common Mistakes
Using self.greet calls the method on the current object, causing recursion.
Calling Parent.greet without an instance will cause an error.
Using greet alone calls the current method, causing infinite recursion.
2fill in blank
medium

Complete the code to call the parent method with arguments.

Ruby
class Parent
  def greet(name)
    puts "Hello, #{name} from Parent"
  end
end

class Child < Parent
  def greet(name)
    [1]
  end
end

Child.new.greet("Alice")
Drag options to blanks, or click blank then click option'
Asuper()
Bsuper(name)
Csuper
Dsuper.greet(name)
Attempts:
3 left
💡 Hint
Common Mistakes
Using super without arguments passes all arguments automatically, but here explicit passing is clearer.
Using super() calls the parent method without arguments, causing errors.
Using super.greet(name) is invalid syntax.
3fill in blank
hard

Fix the error in calling the parent method inside a module included in a class.

Ruby
module Mixin
  def greet
    [1]
  end
end

class Parent
  def greet
    puts "Hello from Parent"
  end
end

class Child < Parent
  include Mixin
end

Child.new.greet
Drag options to blanks, or click blank then click option'
Aself.greet
BParent.greet
Csuper()
Dsuper
Attempts:
3 left
💡 Hint
Common Mistakes
Calling Parent.greet directly causes an error because it's an instance method.
Using self.greet causes infinite recursion.
Using super() without arguments is valid but same as super here.
4fill in blank
hard

Fill the blank to call the parent method and add extra behavior.

Ruby
class Parent
  def greet
    puts "Hello from Parent"
  end
end

class Child < Parent
  def greet
    [1]
    puts "And hello from Child"
  end
end

Child.new.greet
Drag options to blanks, or click blank then click option'
Asuper
Bself.greet
CParent.greet
Dsuper()
Attempts:
3 left
💡 Hint
Common Mistakes
Using self.greet causes infinite recursion.
Calling Parent.greet directly is invalid without an instance.
5fill in blank
hard

Fill the blank to override a method, call the parent method with arguments, and add extra output.

Ruby
class Parent
  def greet(name)
    puts "Hello, #{name} from Parent"
  end
end

class Child < Parent
  def greet(name)
    [1]
    puts "And hello from Child to #{name}"
  end
end

Child.new.greet("Bob")
Drag options to blanks, or click blank then click option'
Asuper(name)
Bself.greet(name)
Csuper()
Dsuper
Attempts:
3 left
💡 Hint
Common Mistakes
Using self.greet(name) causes infinite recursion.
Using super() without arguments causes errors here.