0
0
Rubyprogramming~10 mins

Method_added hook 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 method that prints a message when a new method is added.

Ruby
class MyClass
  def self.method_added([1])
    puts "New method added: #{method_name}"
  end

  def greet
    puts 'Hello!'
  end
end
Drag options to blanks, or click blank then click option'
Aname
Bmethod
Cmethod_name
Dmethod_added
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong parameter name that doesn't represent the method's name.
Forgetting to define the parameter in the method signature.
2fill in blank
medium

Complete the code to print the name of each method added to the class.

Ruby
class Tracker
  def self.method_added([1])
    puts "Method added: #{method_name}"
  end

  def example_method
  end
end
Drag options to blanks, or click blank then click option'
Aadded_method
Bmethod
Cname
Dmethod_name
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name that doesn't match the variable used inside the method.
Not defining any parameter, causing an error.
3fill in blank
hard

Fix the error in the method_added hook to correctly print the added method's name.

Ruby
class Fixer
  def self.method_added([1])
    puts "Added method: #{method_name}"
  end

  def test
  end
end
Drag options to blanks, or click blank then click option'
Amethod
Bmethod_name
Cname
Dadded
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatch between parameter name and variable used inside the method.
Omitting the parameter entirely.
4fill in blank
hard

Fill both blanks to create a method_added hook that ignores methods starting with an underscore.

Ruby
class IgnoreUnderscore
  def self.method_added([1])
    return if method_name.to_s.start_with?([2])
    puts "Added: #{method_name}"
  end

  def _hidden
  end

  def visible
  end
end
Drag options to blanks, or click blank then click option'
Amethod_name
B'_'
C'__'
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong parameter name.
Checking for the wrong prefix string.
5fill in blank
hard

Fill all three blanks to create a method_added hook that tracks added methods in a class variable array.

Ruby
class Tracker
  @methods = []

  def self.method_added([1])
    @methods [2] [3]
  end

  def first
  end

  def second
  end
end
Drag options to blanks, or click blank then click option'
Amethod_name
B<<
D+=
Attempts:
3 left
💡 Hint
Common Mistakes
Using += which creates a new array instead of modifying the existing one.
Using wrong parameter names.