0
0
Rubyprogramming~20 mins

Send for calling methods dynamically in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Send 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 code using send?

Consider the following Ruby code snippet:

class Calculator
  def add(a, b)
    a + b
  end
end

calc = Calculator.new
result = calc.send(:add, 5, 7)
puts result

What will be printed when this code runs?

Ruby
class Calculator
  def add(a, b)
    a + b
  end
end

calc = Calculator.new
result = calc.send(:add, 5, 7)
puts result
Anil
B57
CError: undefined method 'add'
D12
Attempts:
2 left
💡 Hint

Remember that send calls the method named by the symbol with given arguments.

Predict Output
intermediate
2:00remaining
What error does this code raise when using send with a private method?

Look at this Ruby code:

class Secret
  private
  def whisper
    "secret message"
  end
end

s = Secret.new
puts s.send(:whisper)

What will happen when this code runs?

Ruby
class Secret
  private
  def whisper
    "secret message"
  end
end

s = Secret.new
puts s.send(:whisper)
Asecret message
BNoMethodError
CArgumentError
DNameError
Attempts:
2 left
💡 Hint

Does send allow calling private methods?

🔧 Debug
advanced
2:00remaining
Why does this send call raise an error?

Examine this Ruby code:

class Person
  def greet(name)
    "Hello, #{name}!"
  end
end

p = Person.new
p.send(:greet)

Why does this code raise an error?

Ruby
class Person
  def greet(name)
    "Hello, #{name}!"
  end
end

p = Person.new
p.send(:greet)
ATypeError because send cannot call instance methods
BArgumentError because greet expects 1 argument but none given
CSyntaxError due to missing parentheses
DNoMethodError because greet is private
Attempts:
2 left
💡 Hint

Check the method parameters and how send is called.

🧠 Conceptual
advanced
2:00remaining
Which option correctly uses send to call a method with a block?

Given this Ruby code:

class Repeater
  def repeat(times)
    result = []
    times.times { result << yield }
    result
  end
end

r = Repeater.new

Which send call correctly invokes repeat with argument 3 and a block that returns "hi"?

Ar.send(:repeat, 3) { "hi" }
Br.send(:repeat) { 3; "hi" }
Cr.send(:repeat, 3, &proc { "hi" })
Dr.send(:repeat, 3, block: -> { "hi" })
Attempts:
2 left
💡 Hint

How do you pass a block to a method called by send?

Predict Output
expert
2:00remaining
What is the output of this dynamic method call with method_missing?

Consider this Ruby code:

class Dynamic
  def method_missing(name, *args)
    "Called #{name} with #{args.join(", ")}"
  end
end

d = Dynamic.new
puts d.send(:hello, 1, 2, 3)

What will be printed?

Ruby
class Dynamic
  def method_missing(name, *args)
    "Called #{name} with #{args.join(", ")}"
  end
end

d = Dynamic.new
puts d.send(:hello, 1, 2, 3)
ANoMethodError
BArgumentError
CCalled hello with 1, 2, 3
Dnil
Attempts:
2 left
💡 Hint

What happens when a method is missing and send is used?