The method in Ruby is a code block with a name and is a parameterized code associated with one or more objects. The invocation must give the method name, the receiver (the object), the parameter value, and the value of the last expression as the return value. Similar to the Ruby method is the code block, which has no name and is invoked only through iterators or indirectly.
(i) method
(1) Defining methods
Definition method is
Copy Code code as follows:
def method name (argument list)
Method body
End
The last expression is returned as a return value, or returns if there is a value, and no value returns nil. You can omit return.
The method defined in a class is an instance method that can be used on an instance object of a class. If you are defining a method on a particular object, this method is a single key method that can only be used on this object.
Copy Code code as follows:
Class Test
def instance_method_test (v)
Puts "#{v}: Instance method"
End
End
T1=test.new
T2=test.new
T1.instance_method_test ("T1")
T2.instance_method_test ("T2")
def t1.singleton_method_test (v)
Puts "#{v}:singleton method"
End
T1.singleton_method_test ("T1")
#t2. Singleton_method_test ("T2")
In Ruby, the method starts with a lowercase letter and, if more than one word, is separated by an underscore (see habit). Interestingly, the method name can end with an equal sign, a question mark, an exclamation point, and have different meanings: the end of the equal sign (=) indicates that this is an assignment method (write property); The end represents an assertion method that can answer the caller's value for the problem, such as the size of the judgment, the exclamation point (!). The end representation is a mutable method, May affect the state of the object, be careful to use it. These three symbols are interesting, but they are not necessary.
(2) Cancellation method
You can cancel a method by undef the method name. For inherited methods, undef can remove inherited methods from subclasses, but this method in the parent class is not canceled.
(3) method parameter
You can set a default value for this parameter by adding an equal sign and a value in the parameter. If the parameter has a default value, you can specify a value or unspecified value for the parameter when calling the method.
Copy Code code as follows:
def createrole (name,level=1)
Puts ' role name Is#{:name} ' is #{level} '
End
Createrole ("Way")
Createrole ("Clound", 5)
By adding an asterisk (*) at the front of the argument, you can specify the method as a variable number of parameters (array parameters), with a maximum number of digits specified.
Copy Code code as follows:
def Add_person (*users)
Puts users
End
Add_person ("A1", "A2")
Add_person ("A1", "A2", "A3")
If you want to pass the array to the method, asterisking the number (*) before the array.
(ii) code block
method calls can be followed by a code block, and the method of associating code blocks can be invoked by yield.
Copy Code code as follows:
def showmessage (i)
Yield i*2
Print I
End
ShowMessage (5) {|x|print "#{x}"}
The code block is called by yield, or it can be passed as a method parameter, but only if the parameter needs to be added & prefixed and the last argument. At this point it is passed as a Proc object and cannot be invoked by yield, but by the call method of Proc. In addition, if you pass the Proc object to the method in the display mode, the parameter of the method definition is not prefixed with the & prefix. With the & prefix, a code block can be associated with any method call, even if the method does not have a yield statement. Any method call can use the & parameter as the last argument. All methods that support To_proc can use &.
Copy Code code as follows:
#传递代码块, the code block parameter must be the last and add the & prefix; proc Call method
def fun_block (I,&b)
B.call (i*2)
End
Fun_block (2) {|x|puts x}
#显示传递proc对象. Then the parameter of the Proc object part of the method is not prefixed with & prefix
def fun_proc (I,b)
B.call (i*2)
End
P=proc.new{|x|puts x}
Fun_proc (2,P)
c=[1,2,3,4,5,6]
B=c.select{|x|x%2==0}
P1=proc.new{|x|x%2==0}
D=c.select (&P1)
Puts D
A code block is a syntactic structure of ruby, not an object, but you can create an object to represent a block of code. Divides into proc or lambda, depending on how the object is created. The proc behavior is similar to the code block; The lambda behavior is similar to the method, but they are all instances of the Proc class.
Create a proc by proc.new, in version 1.9, synonymous with the Proc method, and create a lambda through the lambda method. The lambda method takes no arguments, and the calling time is associated with a code block.
In Ruby 1.9, a new syntax is supported. Swap the lambda for->; put the arguments outside the curly braces, inside the parentheses; only the expressions are preserved in curly braces. (Can be compared with a lambda in. net3.*: () =>{})
Copy Code code as follows:
#1.8k
D1=LAMBDA{|X|X+1}
Puts D1.call (5)
#1.9, swap the lambda with the->; parameter outside the curly braces, inside the parentheses; only the expressions are preserved in curly braces
The lambda expression in #与. net3.* is much like () =>{}
D2=-> (x) {x+1}
D2.call (5)
This new syntax makes the code concise and unifies the code snippet with the Ruby method, such as setting the default value for the parameter.
(iii) Closures
Both the proc and the Lambda in Ruby are closures (closure). Closures indicate that an object is both a callable function and a variable bound to that function. On the surface, the closure is: Method B in Method a invokes the variable of method A and returns the result. Method B is the closure. Its role: one is that the variables of the B method can be used outside of the method; the second is that the variables in a method are safe to use; the third is to cache the values in the B method.
(about closures, you can view the relevant documents, I am here just superficial understanding.) )