dscher
dscher

Reputation: 1545

Difference between using send method and dot method

The book The Well Grounded Rubyist states that you could use the send method as such to check if an object (ticket) responds to user input:

if ticket.respond_to?(request) 
  puts ticket.send(request)
else
  puts "No such information available"
end  

What is the difference between the code above and writing:

if ticket.respond_to?(request) 
  puts ticket.request
else
  puts "No such information available"
end  

If ticket responds to the user input, why not just call it directly using the dot notation?

Upvotes: 2

Views: 359

Answers (1)

Chuck
Chuck

Reputation: 237110

ticket.request sends the message request to the ticket object.

ticket.send(request) sends whatever is contained in the variable request to the ticket object. So if you had written request = :clone before this, that line would be equivalent to ticket.clone.

Upvotes: 7

Related Questions