Ruby iterative map concise writing implementation principle analysis, rubymap writing principle
Simple Method usage
If you have a string list, you need to convert each string in uppercase. You can use a simple method to complete the conversion.
Copy codeThe Code is as follows:
Name_list = ["chareice", "angel"]
Name_list.map (&: upcase)
# => ["CHAREICE", "ANGEL"]
This statement is equivalent
Copy codeThe Code is as follows:
Name_list.map do {| name. upcase}
Simple Writing brings about a significant increase in efficiency, but what is the principle behind this seemingly magical parameter?
& Symbol
If the & Symbol of the above method call is removed, it is obvious that the: upcase symbol is uploaded to the method as a parameter of the method.
In fact, the & symbol represents the transformation of a block to Proc (block-to-proc conversion ). Let's take a look at the example below.
Copy codeThe Code is as follows:
Def capture_block (& block)
Block. call
End
Capture_block {puts "I have a donkey and I never ride it. "}
# => I have a donkey and I never ride it.
Run the capture_block function and pass a code block to it. The code block is converted into a Proc object and passed to the function through the & symbol Conversion. In the preceding example, the block variable is used. If we output the block class, the output result will be Proc.
You can also pass a Proc object to capture_block to replace the code block.
Copy codeThe Code is as follows:
P = Proc. new {puts "give another donkey "}
Capture_block (& p)
# => Give another donkey
Here, the & symbol is redundant and can be removed completely. The running result is the same.
& What did the symbol do?
Take capture_block (& p) as an example.
1. Trigger the to_proc method of p.
2. Tell the Ruby interpreter to use the result returned by the to_proc method as the block of this function call.
If both the & symbol and the block are used to pass in a function, Ruby reports an error.
Copy codeThe Code is as follows:
Capture_block (& p) {puts "pass to a block "}
#=> SyntaxError: (irb): 30: both block arg and actual block given
Therefore, to pass a Proc object to the & symbol, it will call the to_proc method of the Proc object, return itself, and then pass it to the method as a block of method call.
&: What is upcase?
After knowing the role of the & symbol, we can see that &: upcase first calls the to_proc method of the upcase object.
: The upcase to_proc method is implemented as follows:
Copy codeThe Code is as follows:
Class Symbol
Def to_proc
Proc. new {| obj. send (self )}
End
End
The result is very clear. Symbol # to_proc will return a Proc object with parameters. What the Proc object does is to send a method that calls the method named this Symbol to the object using this Proc object.
#
<S: iterator value = "map. keySet () "id =" key "> <s: iterator value =" map. get (# key) "id =" str "> ..... </s: iterator>
Freemarker map Iteration
FreeMarker template error! Expected collection or sequence. stu evaluated. Why bother writing it? I will give you a way to write.