有人可以解释 Ruby 在块中使用管道字符吗?
有人可以向我解释 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
大括号定义一个匿名函数,称为块。管道之间的标记是该块的参数。所需参数的数量取决于块的使用方式。每次评估块时,需要该块的方法都会根据调用它的对象传递一个值。
它与定义方法相同,只是它不会存储在接受块的方法之外。
例如:
执行时将执行与此相同的操作:
唯一的区别是块是动态定义的而不是存储的。
示例2:
以下语句是等效的
我们使用匿名块,因为作为块传递的大多数函数通常特定于您的情况,不值得定义以供重用。
那么当一个方法接受一个块时会发生什么?这取决于方法。接受块的方法将通过以明确定义的方式从其调用对象传递值来调用它。返回的内容取决于需要块的方法。
例如:在
25.times {|i| 中put i}
.times 对于 0 与其调用者的值之间的每个值调用该块一次,将该值作为临时变量 i 传递到该块中。 Times 返回调用对象的值。在本例中为 25。让我们看一下接受带有两个参数的块的方法。
在这种情况下,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:
will do the same as this when executed:
the only difference is the block is defined on the fly and not stored.
Example 2:
The following statements are equivalent
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.
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.
管道指定参数,这些参数由调用块的函数填充值。它们可以有零个或多个,您应该使用多少个取决于您调用的方法。
例如,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
块参数遵循与方法参数相同的约定(至少从 1.9 开始):您可以定义可选参数、可变长度参数列表、默认值等。这是一个 相当不错的摘要。
需要注意的一些事情:因为块在它们定义的范围内看到变量,所以如果您传入与现有变量同名的参数,它将“隐藏”它 - 您的块将看到传入的值并原始变量将保持不变。
最后将打印“10”。因为有时即使您没有传入值(即您想确保不会意外破坏周围范围中的变量),这也是理想的行为,您可以在参数列表后的分号后面指定块局部变量名称:
在这里,“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.
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:
Here, 'x' is local to the block, even though no value is passed in.