Rudy Method Analysis

Source: Internet
Author: User

What is a method? In OO programming, we do not want to directly manipulate the data of an object from the outside; on the contrary, the object itself knows how to manipulate itself (when properly requested ).
You may say that we pass a message to an object, and those messages will call certain behaviors or make meaningful replies. These will be within the object we don't need to know or care about.
It occurs when the working mechanism occurs. The object method is the job (or equivalent, the message it can understand) that we allow to execute.

In ruby, we call an object method through dot notation (just like C ++ or Java). The called object is given on the left of the vertex.

Ruby> "abcdef". length
6


Technically, we are calling the length method of the object "abcdef.

Other objects may have a slightly different length interface, or they do not have any. The response of a message is determined during the running of the program, and the behavior also depends on the variable it refers.

Ruby> foo = "abc"
"Abc"
Ruby> foo. length
3
Ruby> foo = ["abcde", "fghij"]
["Abcde", "fghij"]
Ruby> foo. length
2


The meaning of length also changes with the change of the object. in the above example, for the first time we call foo to return its length, which corresponds to a simple string, and there is only one reasonable answer here. the second time, foo represents an array, we may consider its length as 2, 5 or 10; but in general, the most appropriate answer is of course 2 (other types of length should also be easily guessed ).

Ruby> foo [0]. length
5
Ruby> foo [0]. length + foo [1]. length
10


It should be noted that an array understands its own meaning as an array. Some code in Ruby gives them this nature, so their requirements can be automatically passed through a variety
The appropriate method is implemented. A small number of method names that correspond to the concepts we express in natural language can be used as expected by different types of data, which frees programmers from a large number of special function names. this feature of OO programming language (in my opinion, Java is not doing well) is called polymorphism ).

When an object encounters an incomprehensible message, an error occurs ":

Ruby> foo = 5
5
Ruby> foo. length
ERR: (eval): 1: undefined method 'length' for 5 (Fixnum)


Therefore, we need to know what method is accepted by this object, although we do not need to know how this method works.

If you want to assign a parameter to a method, the parameter should be in a pair of brackets,

Object. method (arg1, arg2)


If there is no ambiguity, you can remove the brackets.

Object. method arg1, arg2


Ruby has a special variable self, which points to any object that calls the method. Because "self" is often used, it can be saved for convenience:

Self. method_name (args ...)


Same as this

Method_name (args ...)


In the traditional sense, function calling is simply a form of self-calling. this makes Ruby a pure object-oriented language. of course, for those who cannot figure out that function calls in Ruby are actually object methods, function-based methods look like functions in other languages. if we like them, we can also call them functions, as if they are not real object methods.
 

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.