0
0
Rubyprogramming~20 mins

Why metaprogramming is powerful in Ruby - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Metaprogramming Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Ruby metaprogramming code?

Consider this Ruby code that uses define_method to create methods dynamically. What will it print?

Ruby
class Greeter
  [:hello, :goodbye].each do |method_name|
    define_method(method_name) do |name|
      "#{method_name.to_s.capitalize}, #{name}!"
    end
  end
end

g = Greeter.new
puts g.hello("Alice")
puts g.goodbye("Bob")
AHello, Alice!\nGoodbye, Bob!
Bhello, Alice!\ngoodbye, Bob!
CError: undefined method
DHELLO, Alice!\nGOODBYE, Bob!
Attempts:
2 left
💡 Hint

Look at how define_method uses the symbol and string interpolation.

🧠 Conceptual
intermediate
1:30remaining
Why is metaprogramming powerful in Ruby?

Which of these best explains why metaprogramming is powerful in Ruby?

AIt restricts the use of dynamic variables to prevent bugs.
BIt forces all methods to be defined before the program runs, improving performance.
CIt allows programs to write or modify code during runtime, enabling flexible and DRY designs.
DIt automatically converts Ruby code to machine code for faster execution.
Attempts:
2 left
💡 Hint

Think about what it means to change code while the program is running.

🔧 Debug
advanced
2:30remaining
Find the error in this metaprogramming code

This Ruby code tries to create getter methods dynamically but raises an error. What is the cause?

Ruby
class Person
  [:name, :age].each do |attr|
    define_method(attr) do
      @attr
    end
  end
end

p = Person.new
p.name
AThe instance variable should be @name or @age, not @attr.
BYou cannot use define_method inside a class.
CThe array [:name, :age] should be strings, not symbols.
DMissing parentheses in define_method call.
Attempts:
2 left
💡 Hint

Check what @attr means inside the method.

📝 Syntax
advanced
1:30remaining
Which option correctly defines a method dynamically with parameters?

Which Ruby code correctly defines a method greet that takes a name parameter and returns a greeting?

Adefine_method(:greet) |name| { "Hello, #{name}!" }
Bdefine_method greet(name) { "Hello, #{name}!" }
Cdefine_method(:greet) do name; "Hello, #{name}!" end
Ddefine_method(:greet) { |name| "Hello, #{name}!" }
Attempts:
2 left
💡 Hint

Remember the syntax for define_method with a block and parameters.

🚀 Application
expert
3:00remaining
What is the output of this Ruby metaprogramming example?

This Ruby code uses method_missing to handle undefined methods. What will it print?

Ruby
class Dynamic
  def method_missing(name, *args)
    if name.to_s.start_with?("say_")
      word = name.to_s.split("_")[1]
      "You said: #{word} with args: #{args.join(", ")}"
    else
      super
    end
  end
end

d = Dynamic.new
puts d.say_hello("world")
puts d.say_goodbye("everyone")
puts d.unknown_method
AYou said: say_hello with args: world\nYou said: say_goodbye with args: everyone\nNoMethodError
BYou said: hello with args: world\nYou said: goodbye with args: everyone\nNoMethodError
CNoMethodError\nNoMethodError\nNoMethodError
DYou said: hello with args: world\nYou said: goodbye with args: everyone\nYou said: unknown with args:
Attempts:
2 left
💡 Hint

Look at how method_missing handles method names starting with say_.