What is called a method? In OO programming, we don't want to manipulate the data of an object directly from the outside, but instead, the object itself knows how to manipulate itself (when properly asked).
You might say that we pass a message to an object, and those messages invoke a certain kind of behavior or make a meaningful response. It's all in the way that we don't need to know or care about this object.
The working mechanism of the case takes place. For the job we allow to execute (or the equivalent of the message it understands) is the method of the object.
In Ruby, we call a method of an object through dots (dot notation, just like C + + or Java). The called object is given at the left of the point.
Ruby> "abcdef". Length
6
On the face of it, the string is asked about its length. Technically, we're invoking the length method of the object "ABCdef".
Other objects may have a slightly different length interface, or none at all. How the message is answered is determined during the execution of the program, and the behavior depends on the variable it refers to.
The meaning of length changes as the object changes. In the example above, the first time we call Foo to return its length, which corresponds to a simple string, there is only one reasonable answer. For the second time, Foo represents an array, and we may consider its length to be 2, 5 or 10; But generally the most appropriate answer is 2 (other types of length should also be easily guessed).
It is important to note that an array understands its own meaning as an array. Some of the code in Ruby gives them this property, so their needs can be automatically passed through various
The appropriate method is implemented. Because a relatively small number of method names that correspond to the concepts we express in natural language can be used by different kinds of data as we wish, this frees the programmer from a large number of special function names. This feature of the OO programming language (which, in my view, is not doing very well in Java) is called polymorphism (polymorphism).
When an object encounters an incomprehensible message, an error "occurs":
So we need to understand what methods are accepted by this object, although we don't need to know how this approach works.
If you want to assign an argument to a method, the argument should be within a pair of parentheses,
Object.Method (Arg1, arg2)
Parentheses can be removed if ambiguity is not caused.
Object.Method Arg1, arg2
Ruby has a special variable self; It points to an object that calls the method arbitrarily. Because "self" is often used, for convenience it can be omitted:
Self.method_name (args ...)
Same as this.
Method_name (args ...)
Our traditional function calls are simply shorthand for the self method invocation. This also makes Ruby a pure object-oriented language. Of course, for those of you who can't figure out what a function call is in Ruby, the object method, A functional approach looks much like a function in another language. If we like, we can call them functions as if they weren't real object methods.
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.