Methods and code blocks
In Ruby, the code between {} Or do... end is a code block.
A code block can only appear behind a method. It is followed by the same line of the last parameter of the method. Generally, the yield keyword calls the code in the code block.
The method is a famous code block and a parameterized code associated with one or more objects.
When calling a method, you must provide the method name, object (recipient), and zero or multiple parameter values. The value of the last expression in the method will be the return value of the method call.
A code block is not an operable ruby object. Generally, we use a Proc object to represent a code block.
There are two ways of Proc objects: proc and lambda. They are all closures: they will keep the local variables within the scope of the definition, these variables can be accessed even when the external scope is called.
The definition of the method is not mentioned, as mentioned above.
The method name starts with a lowercase letter. If the method name exceeds one letter, it is generally separated by an underscore.
The optional parentheses of the method can be omitted in many method calls, for example, puts "hello" and puts ("hello") are the same.
Ruby is a strong object-oriented language. Its objects are fully encapsulated. The only way to communicate with them is to call their methods, So len = "hello ". length is actually len = "hello ". length (), but the parentheses are omitted. It seems like his attribute is accessed.
Generally, it is recommended that parentheses be omitted if more than one parameter is specified.
The traditional method of code block parameters is that the code block directly follows the method and uses yield to call the code block.
There is a yield statement in the function, and the block outside the function can be executed at that time. In addition, this block can have its own context. It feels a bit like callback, and a bit like macro definition in c.
Some people say that yield acts as a placeholder. A function first gives a placeholder, which cannot be called directly just like a pure function, this function can be used only when this bit pitfall is added with block.
Def block_test (num) if block_given? # Yield # No parameter yield (num) elseputs numendend # block_test (1) # block_test (2) {puts "this is a block... "} block_test (2) {| x | puts x * 2}
Or add a parameter after the method and use it as the prefix of this parameter. This parameter will point to the code block passed to the method. The value of this parameter is a Proc object, it is called by call instead of yield.
def proc_test(num,&b)if block_given? #b.callb.call(num * 3)elseputs "no block"endend#proc_test(1) #proc_test(1) {puts "this is a block"}proc_test(1) {|x| puts x * 2}
Three ways to create a Proc object: Proc. new
If a "&" symbol is added before the last form parameter of the method, Ruby treats the form parameter as a Proc object.
All Proc objects have a call method. When this method is called, the code block defined when this Proc object is created is run.
If Proc. new does not contain parameters, a proc object In Proc mode is returned. Proc objects include proc and lambda.
If Proc. new contains parameters, a Proc object associated with the code block is returned. This object represents the associated code block.
p = Proc.new { puts "hello,dear..."}def proc_test(&pp)pp.callend
Kernel. lambda
The lambda Method returns a Proc object in lambda mode.
Lambda methods do not contain parameters, but must be associated with a code block during calls.
puts lambda{|x| x + 10}.call(2)
Kernel. proc
This 1.8 is a synonym for lambda and 1.9 is a synonym for Proc. new.
This will not be said.
Code block, proc, lambda return
The return statement in a code block is returned not only from the iterator that calls the code block, but also from the method that calls the iterator.
Def testputs "start..." 2. times {puts "hello,"; return} puts "end..." # do not print end
Proc is similar to a code block, so if you execute a return statement in the called proc, it will try to return from the method where the code block is located.
The return Statement in lambda returns only from lambda itself.
Def pro_fun (msg) puts "start... "p = Proc. new {puts "hello, # {msg}"; return} # returns p. callputs "end... "enddef lambda_fun (msg) puts" start .. "lam = lambda {puts" hello, # {msg} "; return} # only return itself lam. callputs "end... "enddef testputs" test start "# pro_fun" song "lambda_fun" song "puts" test end "endtest