0
0
Rubyprogramming~5 mins

Send for calling methods dynamically in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the send method do in Ruby?
The send method calls another method by name dynamically, allowing you to invoke methods using a symbol or string representing the method's name.
Click to reveal answer
beginner
How do you use send to call a method named greet on an object obj?
You write obj.send(:greet). This calls the greet method on obj dynamically.
Click to reveal answer
intermediate
Can send pass arguments to the method it calls? How?
Yes. You pass arguments after the method name symbol. For example, obj.send(:add, 5, 3) calls add(5, 3) on obj.
Click to reveal answer
intermediate
What is the difference between send and public_send?
send can call any method, including private ones. public_send only calls public methods and will raise an error if the method is private.
Click to reveal answer
beginner
Why is send useful in Ruby programming?
It allows flexible and dynamic method calls, useful when method names are not known until runtime, like in metaprogramming or when handling user input.
Click to reveal answer
What argument type does send expect for the method name?
ASymbol or String
BInteger
CBoolean
DArray
Which method allows calling private methods: send or public_send?
ANeither
Bpublic_send
Csend
DBoth
What happens if you use public_send to call a private method?
AIt calls the method successfully
BIt calls the method but warns
CIt returns nil
DIt raises a NoMethodError
How do you pass arguments to a method called with send?
AAs a hash after the method name
BAs additional parameters after the method name
CYou cannot pass arguments with <code>send</code>
DOnly one argument is allowed
Why might you use send instead of calling a method directly?
ATo call methods dynamically when the method name is stored in a variable
BTo improve performance
CTo avoid syntax errors
DTo make code shorter
Explain how the send method works in Ruby and give an example of calling a method with arguments.
Think about how you can call a method when you only have its name as a string or symbol.
You got /4 concepts.
    Describe the difference between send and public_send and when you might use each.
    Consider which method respects Ruby's method visibility rules.
    You got /4 concepts.