在 Ruby 中,垂直线是什么? [英] In Ruby, what are the vertical lines?
本文介绍了在 Ruby 中,垂直线是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
1.upto(9) { |x| print x }
为什么这行不通?
{ print x |x} }
y
怎么样?
推荐答案
用于传递给块的参数.即在您的示例中,upto
将使用从 1 到 9 的每个数字调用您的块,当前值可用作 x
.
It's for the parameters that are being passed to your block. i.e. in your example, upto
will call your block with each number from 1 to 9 and the current value is available as x
.
块参数可以有任何名称,就像方法参数一样.例如1.upto(9) { |num|puts num }
有效.
The block parameters can have any name, just like method parameters. e.g. 1.upto(9) { |num| puts num }
is valid.
就像方法的参数一样,一个块也可以有多个参数.例如
Just like parameters to a method you can also have multiple parameters to a block. e.g.
hash.each_pair { |key, value| puts "#{key} is #{value}" }
这篇关于在 Ruby 中,垂直线是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文