ruby 块_Ruby块

超、凢脫俗 2023-03-05 06:28 20阅读 0赞

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,

考虑以下代码,

  1. 7.times do
  2. puts "Oh, hello from inside a block. You are at Includehelp!"
  3. end

Output

输出量

  1. Oh, hello from inside a block. You are at Includehelp!
  2. Oh, hello from inside a block. You are at Includehelp!
  3. Oh, hello from inside a block. You are at Includehelp!
  4. Oh, hello from inside a block. You are at Includehelp!
  5. Oh, hello from inside a block. You are at Includehelp!
  6. Oh, hello from inside a block. You are at Includehelp!
  7. 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:

句法:

  1. name_of_block do
  2. #expression-1
  3. #expression-2
  4. end

Example:

例:

  1. ["C++", "Ruby", 155,"Includehelp","Java","Python",789,889.6].each do |k|
  2. puts k
  3. end

Output

输出量

  1. C++
  2. Ruby
  3. 155
  4. Includehelp
  5. Java
  6. Python
  7. 789
  8. 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:

句法:

  1. name_of_the_block{ #expression_to_be_executed }

Example:

例:

  1. ["C++", "Ruby", 155,"Includehelp","Java","Python",789,889.6].each {
  2. |k|
  3. puts k }

Output

输出量

  1. C++
  2. Ruby
  3. 155
  4. Includehelp
  5. Java
  6. Python
  7. 789
  8. 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:

例:

  1. # method Includehelp
  2. def Includehelp
  3. # expressions to be executed
  4. puts "Inside Includehelp!"
  5. # yield statement is used
  6. # Tutorial is the parameter
  7. yield "Tutorial"
  8. # expression to be executed
  9. puts "Again Inside Includehelp!"
  10. # using yield statement
  11. # Tutorial at Includehelp.com is the parameter
  12. yield "Tutorial at Includehelp.com"
  13. end
  14. # block
  15. Includehelp{
  16. |example| puts "Inside Block #{example}"}

Output

输出量

  1. Inside Includehelp!
  2. Inside Block Tutorial
  3. Again Inside Includehelp!
  4. 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语句的位置继续执行。

翻译自: https://www.includehelp.com/ruby/blocks.aspx

ruby 块

发表评论

表情:
评论列表 (有 0 条评论,20人围观)

还没有评论,来说两句吧...

相关阅读

    相关 ruby 的代码和yield

    =begin yield的意思其实很简单,就是暂停代码的运行,将yield后面的参数传递给block,block根据传递来的参数运行 当block返回值得时候,值

    相关 Ruby 中的闭包-代码

    在许多编程语言中都会有闭包这个概念。今天主要来谈谈Ruby中的闭包,它在这门语言中地位如何,以什么形式存在,主要用途有哪些? 闭包概要 维基百科里对闭包地解释如下