Send for calling methods dynamically in Ruby - Time & Space Complexity
When we use send in Ruby to call methods dynamically, it is important to understand how this affects the time it takes to run the program.
We want to know how the number of method calls grows as we call more methods dynamically.
Analyze the time complexity of the following code snippet.
class Greeter
def hello
"Hello!"
end
def goodbye
"Goodbye!"
end
end
g = Greeter.new
methods = [:hello, :goodbye]
methods.each { |m| puts g.send(m) }
This code calls methods dynamically on an object using send inside a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling
sendto invoke a method dynamically. - How many times: Once for each method symbol in the
methodsarray.
Each method call happens once per item in the list of method names.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 method calls via send |
| 100 | 100 method calls via send |
| 1000 | 1000 method calls via send |
Pattern observation: The number of dynamic calls grows directly with the number of methods to call.
Time Complexity: O(n)
This means the time to run grows linearly with the number of methods called dynamically.
[X] Wrong: "Using send makes the code slower in a way that changes the overall time complexity."
[OK] Correct: Calling methods dynamically with send adds a small fixed overhead per call but does not change how the total time grows with the number of calls.
Understanding how dynamic method calls scale helps you explain your code's performance clearly and confidently in real projects or interviews.
What if we replaced the array of method names with a nested loop calling send multiple times per method? How would the time complexity 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
