0
0
Rubyprogramming~10 mins

Why hooks enable framework building 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 hook method that can be overridden.

Ruby
def [1]
  # default behavior
end
Drag options to blanks, or click blank then click option'
Ahook_method
Brun
Cinitialize
Dexecute
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that is too specific or already reserved.
Not defining a method at all.
2fill in blank
medium

Complete the code to call the hook method inside another method.

Ruby
def perform
  puts 'Starting task'
  [1]
  puts 'Task finished'
end
Drag options to blanks, or click blank then click option'
Aexecute_hook
Brun_hook
Chook_method
Dstart_hook
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a method that is not defined.
Forgetting to call the hook method.
3fill in blank
hard

Fix the error in the subclass to override the hook method correctly.

Ruby
class CustomTask < BaseTask
  def [1]
    puts 'Custom behavior'
  end
end
Drag options to blanks, or click blank then click option'
Aperform
Brun
Cinitialize
Dhook_method
Attempts:
3 left
💡 Hint
Common Mistakes
Overriding the wrong method name.
Not overriding the hook method at all.
4fill in blank
hard

Fill both blanks to define a base class with a hook and a method that calls it.

Ruby
class BaseTask
  def [1]
    # default hook
  end

  def [2]
    puts 'Begin'
    [1]
    puts 'End'
  end
end
Drag options to blanks, or click blank then click option'
Ahook_method
Bperform
Cexecute
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up method names.
Not calling the hook inside the action method.
5fill in blank
hard

Fill all three blanks to create a subclass that overrides the hook and runs the task.

Ruby
class CustomTask < BaseTask
  def [1]
    puts 'Custom action'
  end
end

task = CustomTask.new
[2]
task.[3]
Drag options to blanks, or click blank then click option'
Ahook_method
Btask
Cperform
Dnew
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method names.
Not creating an instance before calling perform.