Respond_to? In Ruby? And send usage

Source: Internet
Author: User

From: http://galeki.is-programmer.com/show/183.html

Like other oo languages, in ruby, object functions are completed by sending messages to objects, such as Str. upcase is the message that sends upcase to str. The vertex operator (.), it is used to send messages to objects. Str receives the messages and then executes the corresponding functions of the messages.

However, in some cases, we do not know which messages the object can respond to. For example, the following code produces an error:

  1. > OBJ = object. New
  2. > Obj. Talk
  3. Undefined method 'tal' for # <object: 0x12345678> (nomethoderror)

 

Because the OBJ object cannot respond to the talk message, if respond_to? This method can be used to determine whether an object can respond to a given message:

  1. OBJ = object. New
  2. If obj. respond_to? ("Talk ")
  3. OBJ. Talk
  4. Else
  5. Puts "sorry, object can't talk! "
  6. End

 

In this way, even if OBJ cannot respond to talk, it will not cause code errors to exit. We can also apply respond_to? Method, flexible control during program running based on object attributes.

And respond_to? Correspondingly, the send method is the same as the dot operator to send messages to objects, such as STR at the beginning of the article. upcase, which can be written as STR by sending. send ("upcase"), they implement the same functions, so why should we use send?

This is because the send message is variable when the program is running. We can dynamically send different messages to the object based on different inputs.

For example, in a book management system, each book has attributes such as author, publisher, date, and price. We need to query the attributes of a book based on user input. If you do not need to send, we need to test the input of the program one by one:

  1. Print "Search :"
  2. Request = gets. Chomp
  3. If request = "Writer"
  4. Puts book. Writer
  5. Elsif request = "Press"
  6. Puts book. Press
  7. Elseif request = "date"
  8. Puts book. Date
  9. ......

If the send method is used, it is much simpler:

  1. Request = gets. Chomp
  2. If book. respond_to? (Request)
  3. Puts book. Send (request)
  4. Else
  5. Puts "Input error"
  6. End

In this way, you do not need to test user input one by one, as long as the query object can correspond to this message, and then send the input directly to the object.

Through respond_to? And send. We can construct more flexible and stable programs.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.