Bird
Raised Fist0
Rubyprogramming~20 mins

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

Choose your learning style10 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?

Practice

(1/5)
1. What does the Ruby method send do when used like obj.send(:method_name)?
easy
A. Calls the method named method_name on obj dynamically.
B. Creates a new method called method_name on obj.
C. Deletes the method named method_name from obj.
D. Returns the name of the method as a string.

Solution

  1. Step 1: Understand send usage

    The send method calls a method on an object using the method's name given as a symbol or string.
  2. Step 2: Analyze the example obj.send(:method_name)

    This calls the method named method_name on obj dynamically at runtime.
  3. Final Answer:

    Calls the method named method_name on obj dynamically. -> Option A
  4. Quick Check:

    send calls methods dynamically [OK]
Hint: Remember: send calls methods by name on objects [OK]
Common Mistakes:
  • Thinking send creates or deletes methods
  • Confusing send with method name retrieval
  • Assuming send returns method names as strings
2. Which of the following is the correct syntax to call a method named greet on object user using send?
easy
A. user.send(greet)
B. user.send('greet')()
C. user.send['greet']
D. user.send(:greet)

Solution

  1. Step 1: Recall send syntax

    The method name should be passed as a symbol or string without parentheses after send.
  2. Step 2: Check each option

    user.send(greet) misses quotes or symbol, causing error. user.send(:greet) correctly uses a symbol :greet. user.send('greet')() wrongly adds parentheses after send call. user.send['greet'] uses incorrect bracket syntax.
  3. Final Answer:

    user.send(:greet) -> Option D
  4. Quick Check:

    Use symbol for method name in send [OK]
Hint: Use colon before method name symbol in send [OK]
Common Mistakes:
  • Passing method name without quotes or colon
  • Adding parentheses after send call
  • Using brackets [] instead of parentheses ()
3. What is the output of this Ruby code?
class Person
  def greet(name)
    "Hello, #{name}!"
  end
end

p = Person.new
puts p.send(:greet, "Alice")
medium
A. Hello, Alice!
B. greet
C. NoMethodError
D. Hello, name!

Solution

  1. Step 1: Understand the method call with send

    The send method calls greet on p with argument "Alice".
  2. Step 2: Evaluate the method output

    The greet method returns "Hello, Alice!" which is printed by puts.
  3. Final Answer:

    Hello, Alice! -> Option A
  4. Quick Check:

    send calls greet with argument "Alice" [OK]
Hint: send passes arguments after method name [OK]
Common Mistakes:
  • Expecting method name string as output
  • Confusing argument passing with no arguments
  • Thinking send returns method object
4. Identify the error in this Ruby code snippet:
class Calculator
  def add(a, b)
    a + b
  end
end

calc = Calculator.new
calc.send(:add, 5)
medium
A. send cannot be used with arguments
B. Method add does not exist
C. Missing second argument for method add
D. Syntax error in method definition

Solution

  1. Step 1: Check method definition

    The method add requires two arguments: a and b.
  2. Step 2: Analyze the send call

    The send call provides only one argument (5), missing the second argument.
  3. Final Answer:

    Missing second argument for method add -> Option C
  4. Quick Check:

    Method arguments must match when using send [OK]
Hint: Match method arguments count when using send [OK]
Common Mistakes:
  • Passing fewer arguments than method requires
  • Assuming send works without correct arguments
  • Thinking send changes method signature
5. You want to call different arithmetic methods (add, subtract, multiply) on a Calculator object dynamically based on user input stored in operation. Which Ruby code correctly uses send to achieve this?
class Calculator
  def add(a, b); a + b; end
  def subtract(a, b); a - b; end
  def multiply(a, b); a * b; end
end

calc = Calculator.new
operation = :multiply
# Fill in the blank below to call the method dynamically
result = ???
puts result
hard
A. calc.send(:operation, 4, 5)
B. calc.send(operation, 4, 5)
C. calc.send('operation', 4, 5)
D. calc.send(operation())

Solution

  1. Step 1: Understand dynamic method calling

    The variable operation holds the symbol of the method to call.
  2. Step 2: Use send with variable method name and arguments

    Passing operation as the first argument to send calls the correct method with arguments 4 and 5.
  3. Final Answer:

    calc.send(operation, 4, 5) -> Option B
  4. Quick Check:

    send uses variable method names with arguments [OK]
Hint: Pass method name variable and args directly to send [OK]
Common Mistakes:
  • Passing method name as string literal instead of variable
  • Adding parentheses after variable name incorrectly
  • Using symbol literal instead of variable for method name
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard