ruby 块
Ruby块 (Ruby blocks)
A block works just like a method but it doesn't have a name. It does not belong to any object. Blocks can also be considered as unspecified chunks of code which can consume input in the form of arguments and return some value. Most importantly, blocks can only be created by the means of passing them to a method when the method is invoked and yield statement is used to invoke a block inside a method along with a value.
块的工作原理与方法相同,但没有名称。 它不属于任何对象。 块也可以被视为未指定的代码块,它们可以消耗参数形式的输入并返回一些值。 最重要的是,只能在调用方法时使用块将其传递给方法的方式来创建块,并使用yield语句将值和值一起调用方法内部的块。
Consider the following code,
考虑以下代码,
7.times do
puts "Oh, hello from inside a block. You are at Includehelp!"
end
Output
输出量
Oh, hello from inside a block. You are at Includehelp!
Oh, hello from inside a block. You are at Includehelp!
Oh, hello from inside a block. You are at Includehelp!
Oh, hello from inside a block. You are at Includehelp!
Oh, hello from inside a block. You are at Includehelp!
Oh, hello from inside a block. You are at Includehelp!
Oh, hello from inside a block. You are at Includehelp!
You can observe in the above code that times is a method that is defined on numbers. 7.times prints the expression 7 times.
您可以在上面的代码中观察到time是一种在数字上定义的方法。 7.times将表达式打印7次。
When times method is invoked only a block is passed which is a named chunk of code between do and end statement.
调用times方法时,仅传递一个块,该块是do和end语句之间的已命名代码块。
No object is participating with the method times.
没有对象参与方法时间 。
块的实现 (Implementation of Blocks)
1) Inside do...end statement
1)在do ... end语句中
It is the most common use of the block. The major portion of the code has this type of implementation.
这是该块的最常见用法。 代码的主要部分具有这种类型的实现。
Syntax:
句法:
name_of_block do
#expression-1
#expression-2
end
Example:
例:
["C++", "Ruby", 155,"Includehelp","Java","Python",789,889.6].each do |k|
puts k
end
Output
输出量
C++
Ruby
155
Includehelp
Java
Python
789
889.6
2) Between curly braces {} (Inline Implementation)
2)在花括号{}之间(内联实现)
The above can be executed without do...end statement. The same can be done by putting the expressions inside the curly braces {}.
无需do ... end语句即可执行以上操作 。 可以通过将表达式放在花括号{}中来完成相同的操作。
Syntax:
句法:
name_of_the_block{ #expression_to_be_executed }
Example:
例:
["C++", "Ruby", 155,"Includehelp","Java","Python",789,889.6].each {|k|
puts k }
Output
输出量
C++
Ruby
155
Includehelp
Java
Python
789
889.6
3)收益声明 (3) The yield statement)
When you want to call a block inside the method, Ruby provides you yield statement for this purpose. The keyword yield is used when you invoke a block inside the method.
当您想在方法内部调用一个块时,Ruby为此提供了yield语句。 当您在方法内部调用一个块时,将使用关键字yield 。
The yield statement can also consume parameters.
yield语句还可以使用参数。
Example:
例:
# method Includehelp
def Includehelp
# expressions to be executed
puts "Inside Includehelp!"
# yield statement is used
# Tutorial is the parameter
yield "Tutorial"
# expression to be executed
puts "Again Inside Includehelp!"
# using yield statement
# Tutorial at Includehelp.com is the parameter
yield "Tutorial at Includehelp.com"
end
# block
Includehelp{ |example| puts "Inside Block #{example}"}
Output
输出量
Inside Includehelp!
Inside Block Tutorial
Again Inside Includehelp!
Inside Block Tutorial at Includehelp.com
In the above code, you can observe that firstly, method expression is invoked which displayed Includehelp. After that when the yield statement is found and executed, the pointer goes inside block for the execution of its statement. After successful execution, the pointer goes back to the method will continue to execute from where the yield statement invoked.
在上面的代码中,您可以观察到,首先,调用了显示Includehelp的方法表达式。 之后,当yield语句被找到并执行时,指针进入block内部以执行其语句。 成功执行后,指针将返回该方法,该方法将从调用yield语句的位置继续执行。
ruby 块