有人可以解释 Ruby 在块中使用管道字符吗?

发布于 2024-10-03 11:38:04 字数 169 浏览 0 评论 0原文

有人可以向我解释 Ruby 在块中使用管道字符吗?据我所知,它包含一个变量名称,该变量名称将在迭代时分配数据。但这叫什么?管道内可以有多个变量吗?还有什么我应该知道的吗?有更多相关信息的好链接吗?

例如:

25.times { | i | puts i }

Can someone explain to me Ruby's use of pipe characters in a block? I understand that it contains a variable name that will be assigned the data as it iterates. But what is this called? Can there be more than one variable inside the pipes? Anything else I should know about it? Any good links to more information on it?

For example:

25.times { | i | puts i }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

把昨日还给我 2024-10-10 11:38:04

大括号定义一个匿名函数,称为块。管道之间的标记是该块的参数。所需参数的数量取决于块的使用方式。每次评估块时,需要该块的方法都会根据调用它的对象传递一个值。

它与定义方法相同,只是它不会存储在接受块的方法之外。

例如:

def my_print(i) 
  puts i
end

执行时将执行与此相同的操作:

{|i| puts i}

唯一的区别是块是动态定义的而不是存储的。

示例2:
以下语句是等效的

25.times &method(:my_print)

25.times {|i| puts i}

我们使用匿名块,因为作为块传递的大多数函数通常特定于您的情况,不值得定义以供重用。

那么当一个方法接受一个块时会发生什么?这取决于方法。接受块的方法将通过以明确定义的方式从其调用对象传递值来调用它。返回的内容取决于需要块的方法。

例如:在 25.times {|i| 中put i} .times 对于 0 与其调用者的值之间的每个值调用该块一次,将该值作为临时变量 i 传递到该块中。 Times 返回调用对象的值。在本例中为 25。

让我们看一下接受带有两个参数的块的方法。

{:key1 => "value1", :key2 => "value2"}.each {|key,value| 
     puts "This key is: #{key}. Its value is #{value}"
}

在这种情况下,each 为每个键/值对调用块,将键作为第一个参数,将值作为第二个参数。

Braces define an anonymous function, called a block. Tokens between the pipe are the arguments of this block. The number of arguments required depends on how the block is used. Each time the block is evaluated, the method requiring the block will pass a value based on the object calling it.

It's the same as defining a method, only it's not stored beyond the method that accepts a block.

For example:

def my_print(i) 
  puts i
end

will do the same as this when executed:

{|i| puts i}

the only difference is the block is defined on the fly and not stored.

Example 2:
The following statements are equivalent

25.times &method(:my_print)

25.times {|i| puts i}

We use anonymous blocks because the majority of functions passed as a block are usually specific to your situation and not worth defining for reuse.

So what happens when a method accepts a block? That depends on the method. Methods that accept a block will call it by passing values from their calling object in a well defined manner. What's returned depends on the method requiring the block.

For example: In 25.times {|i| puts i} .times calls the block once for each value between 0 and the value of its caller, passing the value into the block as the temporary variable i. Times returns the value of the calling object. In this case 25.

Let's look at method that accepts a block with two arguments.

{:key1 => "value1", :key2 => "value2"}.each {|key,value| 
     puts "This key is: #{key}. Its value is #{value}"
}

In this case each calls the block ones for each key/value pair passing the key as the first argument and the value as the second argument.

↙温凉少女 2024-10-10 11:38:04

管道指定参数,这些参数由调用块的函数填充值。它们可以有零个或多个,您应该使用多少个取决于您调用的方法。

例如,each_with_index 使用两个变量,并将元素放入其中一个变量中,将索引放入另一个变量中。

这里很好地描述了块和迭代器的工作原理

The pipes specify arguments that are populated with values by the function that calls your block. There can be zero or more of them, and how many you should use depends on the method you call.

For example, each_with_index uses two variables and puts the element in one of them and the index in the other.

here is a good description of how blocks and iterators work

梦开始←不甜 2024-10-10 11:38:04

块参数遵循与方法参数相同的约定(至少从 1.9 开始):您可以定义可选参数、可变长度参数列表、默认值等。这是一个 相当不错的摘要

需要注意的一些事情:因为块在它们定义的范围内看到变量,所以如果您传入与现有变量同名的参数,它将“隐藏”它 - 您的块将看到传入的值并原始变量将保持不变。

i = 10
25.times { | i | puts i }
puts i #=> prints '10'

最后将打印“10”。因为有时即使您没有传入值(即您想确保不会意外破坏周围范围中的变量),这也是理想的行为,您可以在参数列表后的分号后面指定块局部变量名称:

x = 'foo'
25.times { | i ; x | puts i; x = 'bar' }
puts x #=> prints 'foo'

在这里,“x”对于块来说是本地的,即使没有传入任何值。

Block arguments follow all the same conventions as method parameters (at least as of 1.9): you can define optional arguments, variable length arg lists, defaults, etc. Here's a pretty decent summary.

Some things to be aware of: because blocks see variables in the scope they were defined it, if you pass in an argument with the same name as an existing variable, it will "shadow" it - your block will see the passed in value and the original variable will be unchanged.

i = 10
25.times { | i | puts i }
puts i #=> prints '10'

Will print '10' at the end. Because sometimes this is desirable behavior even if you are not passing in a value (ie you want to make sure you don't accidentally clobber a variable from surrounding scope) you can specify block-local variable names after a semicolon after the argument list:

x = 'foo'
25.times { | i ; x | puts i; x = 'bar' }
puts x #=> prints 'foo'

Here, 'x' is local to the block, even though no value is passed in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文