What if you could call any method just by its name, without writing endless if-else statements?
Why Send for calling methods dynamically in Ruby? - Purpose & Use Cases
Imagine you have a program that needs to call different methods based on user input or some changing conditions. You try to write separate code for each method call manually.
This manual way means writing many if-else or case statements. It gets long, hard to read, and easy to make mistakes. Adding new methods means changing lots of code.
Using send lets you call any method by its name stored in a variable. This means one simple line can replace many if-else checks, making your code cleaner and easier to update.
if action == 'start' start elsif action == 'stop' stop end
send(action)
You can write flexible programs that decide what to do at runtime without messy conditionals.
A game where player commands like 'jump', 'run', or 'attack' are typed, and the program calls the matching method dynamically.
Manual method calls need many condition checks.
send calls methods by name stored in variables.
This makes code shorter, cleaner, and easier to change.
Practice
send do when used like obj.send(:method_name)?Solution
Step 1: Understand
Thesendusagesendmethod calls a method on an object using the method's name given as a symbol or string.Step 2: Analyze the example
This calls the method namedobj.send(:method_name)method_nameonobjdynamically at runtime.Final Answer:
Calls the method namedmethod_nameonobjdynamically. -> Option AQuick Check:
sendcalls methods dynamically [OK]
- Thinking send creates or deletes methods
- Confusing send with method name retrieval
- Assuming send returns method names as strings
greet on object user using send?Solution
Step 1: Recall
The method name should be passed as a symbol or string without parentheses aftersendsyntaxsend.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 aftersendcall. user.send['greet'] uses incorrect bracket syntax.Final Answer:
user.send(:greet) -> Option DQuick Check:
Use symbol for method name in send [OK]
- Passing method name without quotes or colon
- Adding parentheses after send call
- Using brackets [] instead of parentheses ()
class Person
def greet(name)
"Hello, #{name}!"
end
end
p = Person.new
puts p.send(:greet, "Alice")Solution
Step 1: Understand the method call with send
Thesendmethod callsgreetonpwith argument "Alice".Step 2: Evaluate the method output
Thegreetmethod returns "Hello, Alice!" which is printed byputs.Final Answer:
Hello, Alice! -> Option AQuick Check:
send calls greet with argument "Alice" [OK]
- Expecting method name string as output
- Confusing argument passing with no arguments
- Thinking send returns method object
class Calculator
def add(a, b)
a + b
end
end
calc = Calculator.new
calc.send(:add, 5)Solution
Step 1: Check method definition
The methodaddrequires two arguments:aandb.Step 2: Analyze the send call
Thesendcall provides only one argument (5), missing the second argument.Final Answer:
Missing second argument for method add -> Option CQuick Check:
Method arguments must match when using send [OK]
- Passing fewer arguments than method requires
- Assuming send works without correct arguments
- Thinking send changes method signature
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
Solution
Step 1: Understand dynamic method calling
The variableoperationholds the symbol of the method to call.Step 2: Use send with variable method name and arguments
Passingoperationas the first argument tosendcalls the correct method with arguments 4 and 5.Final Answer:
calc.send(operation, 4, 5) -> Option BQuick Check:
send uses variable method names with arguments [OK]
- Passing method name as string literal instead of variable
- Adding parentheses after variable name incorrectly
- Using symbol literal instead of variable for method name
Ready to prove it?
Pass the test and this pattern moves unknown
