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?✗ Incorrect
send expects the method name as a symbol or string to call it dynamically.Which method allows calling private methods:
send or public_send?✗ Incorrect
send can call private methods, but public_send cannot.What happens if you use
public_send to call a private method?✗ Incorrect
public_send raises a NoMethodError if the method is private.How do you pass arguments to a method called with
send?✗ Incorrect
Arguments are passed as additional parameters after the method name symbol or string.
Why might you use
send instead of calling a method directly?✗ Incorrect
send is useful for dynamic method calls when the method name is not known until runtime.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.