0
0
Rubyprogramming~20 mins

Define_method for dynamic methods in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Define_Method Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of dynamic method using define_method
What is the output of this Ruby code that uses define_method to create a dynamic method?
Ruby
class Greeter
  def initialize(name)
    @name = name
  end

  define_method(:greet) do
    "Hello, #{@name}!"
  end
end

g = Greeter.new("Alice")
puts g.greet
ASyntaxError
BHello, @name!
Cundefined method `greet' for #<Greeter:0x0000>
DHello, Alice!
Attempts:
2 left
💡 Hint
Remember that define_method creates an instance method that can access instance variables.
🧠 Conceptual
intermediate
1:30remaining
Understanding scope in define_method blocks
Which statement about the scope of variables inside a define_method block is true?
AThe block has access to local variables from the surrounding method where <code>define_method</code> is called.
BThe block cannot access any variables and must use global variables only.
CThe block runs in the context of the instance, so it can access instance variables but not local variables from outside the block.
DThe block can only access instance variables and method parameters, but not local variables from outside.
Attempts:
2 left
💡 Hint
Think about where the block is executed when the dynamic method is called.
🔧 Debug
advanced
2:00remaining
Why does this define_method code raise an error?
This Ruby code raises an error. What is the cause?
Ruby
class Calculator
  define_method(:add) do |a, b|
    a + b
  end
end

calc = Calculator.new
puts calc.add(5)
ANoMethodError: undefined method `add' for #<Calculator:0x0000>
BArgumentError: wrong number of arguments (given 1, expected 2)
CSyntaxError: unexpected `|'
DTypeError: nil can't be coerced into Integer
Attempts:
2 left
💡 Hint
Check how many arguments the method expects versus how many are given.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in define_method usage
Which option contains a syntax error when using define_method?
Ruby
class Person
  define_method(:say_hello) do
    puts "Hello!"
  end
end
Adefine_method(:say_hello) do puts "Hello!" end
Bdefine_method :say_hello { puts "Hello!" }
Cdefine_method(:say_hello) { puts "Hello!" }
Ddefine_method(:say_hello) do || puts "Hello!" end
Attempts:
2 left
💡 Hint
Check the block syntax used with define_method.
🚀 Application
expert
3:00remaining
Create dynamic getter methods with define_method
Given the following Ruby class, which option correctly uses define_method to create getter methods for each attribute in ATTRIBUTES?
Ruby
class Product
  ATTRIBUTES = [:name, :price, :stock]

  def initialize(name, price, stock)
    @name = name
    @price = price
    @stock = stock
  end

  # Define getter methods dynamically here
end
A
ATTRIBUTES.each do |attr|
  define_method(attr) { instance_variable_get("@#{attr}") }
end
B
ATTRIBUTES.each do |attr|
  define_method(attr) { @attr }
end
C
ATTRIBUTES.each do |attr|
  define_method(attr) { self.attr }
end
D
ATTRIBUTES.each do |attr|
  define_method(attr) { instance_variable_get(attr) }
end
Attempts:
2 left
💡 Hint
Use instance_variable_get with the correct instance variable name string.