send invokes a named method on an object without using the dot operator. Because the method name can come from a variable or runtime input, it turns a message into data that Ruby can dispatch dynamically. section 2.3.3 section 8.2.3
Dispatching a ticket query
A ticket program can read a request such as "venue", check `ticket.respond_to?(request)`, and call `ticket.send(request)` instead of writing a separate conditional for every property. The check prevents unknown requests from being sent blindly. section 2.3.3
Use the related forms according to the method name and visibility you need:
- `__send__` provides the same behavior with a name unlikely to collide with an application-defined `send` method. section 2.3.3
- `public_send` restricts dispatch to public methods, whereas `send` can invoke private methods. section 2.3.3
- Pass a symbol such as `:upcase`, or pass a string such as `"upcase"`; symbols can avoid conversion while Ruby locates the method. section 8.2.3
if ticket.respond_to?(request) 1
puts ticket.send(request)
else
puts "No such information available"
end
Inspect the guarded ticket-query pattern that combines dynamic input, respond_to?, and send. section 2.3.3
