I'm new to Ruby, but not to languages that allow lambda's, such as groovy. So I saw this example:
我是Ruby的新手,但不是那种允许使用lambda的语言,比如groovy。所以我看到了这个例子:
myArray.product(otherArray).reject{|i,j| i > j}
in a ruby code block, and I hadn't seen this block take 2 arguments before, but when I went to look at the documentation I can only see the documentation that says that it takes 1 argument. I looked at the same for the enumerable
class, but that doc only shows 1 argument also.
在ruby代码块中,我之前没有看过这个块有2个参数,但是当我去查看文档时,我只能看到文档说它需要1个参数。我对可枚举的类看了同样的内容,但是doc也只显示了1个参数。
I understand that it works, I guess I was hoping that there was an easier way to determine how many arguments it takes other then a guess and test method. How can I tell how many arguments a block takes in Ruby?
我知道它有效,我想我希望有一种更简单的方法来确定除了猜测和测试方法之外需要多少个参数。如何判断一个块在Ruby中有多少参数?
3 个解决方案
#1
9
This works because Ruby supports destructuring.
这是有效的,因为Ruby支持解构。
Destructuring allows you to bind a set of variables to a corresponding set of values anywhere that you can normally bind a value to a single variable.
解构允许您将一组变量绑定到一组相应的值,您通常可以将值绑定到单个变量。
This allows the following to hold true:
这允许以下内容成立:
arr = [1, 2]
x = arr
x == [1, 2] # true
y, z = arr
y == 1 # true
z == 2 # true
You can see from the following code that destructuring in arguments to blocks isn't unique to the built-in methods that take a block:
您可以从以下代码中看到,块的参数中的解构对于采用块的内置方法不是唯一的:
def my_method(arr)
yield arr
end
my_method([1, 2, 3]) {|x| puts x.inspect }
# => [1, 2, 3]
my_method([1, 2, 3]) {|x, y, z| puts x.inspect }
# => 1
Check out Destructuring with Ruby for more information.
有关更多信息,请查看使用Ruby进行解构。
#2
3
You can do some interesting restructuring in block parameters, depending on the structure of your array:
您可以在块参数中进行一些有趣的重组,具体取决于数组的结构:
[[1, 2], [3, 4], [5, 6], [7, 8]].reject {|x,y| y == 8 }
#=> [[1, 2], [3, 4], [5, 6]]
You can group them in parentheses:
您可以将它们分组在括号中:
[ [[1,2],3], [[1,3],6] ].select {|(x,y),z| x == 1 && z == 3 }
#=> [ [[1,2],3] ]
You can also use the splat operator for various things, like dealing with variable-length subarrays:
您还可以使用splat运算符执行各种操作,例如处理可变长度子数组:
[[:a,:b,2,3,4,5,6], [:c,:d,7,8,9]].each {|x,y,*numbers| puts numbers.inspect }
#=> [2,3,4,5,6]
#=> [7,8,9]
#3
1
Ruby is flexible in how it interprets the arguments; here is a similar example, with one and then two arguments:
Ruby在解释参数方面很灵活;这是一个类似的例子,有一个然后是两个参数:
[1, 3].product([2, 4]).reject {|a| a.first > a.last }
=> [[1, 2], [1, 4], [3, 4]]
[1, 3].product([2, 4]).reject {|a,b| a > b }
=> [[1, 2], [1, 4], [3, 4]]
The rule of thumb here is that you can treat the arguments either as a composite object, or as individual elements in a collection. E.g.,
这里的经验法则是,您可以将参数视为复合对象,也可以视为集合中的单个元素。例如。,
[1, 2, 3].tap {|a,b,c| puts [a,b,c].inspect }
[1, 2, 3]
...
[1, 2, 3].tap {|a,b| puts [a,b].inspect }
[1, 2]
...
[1, 2, 3].tap {|a| puts a.inspect }
[1, 2, 3]
#1
9
This works because Ruby supports destructuring.
这是有效的,因为Ruby支持解构。
Destructuring allows you to bind a set of variables to a corresponding set of values anywhere that you can normally bind a value to a single variable.
解构允许您将一组变量绑定到一组相应的值,您通常可以将值绑定到单个变量。
This allows the following to hold true:
这允许以下内容成立:
arr = [1, 2]
x = arr
x == [1, 2] # true
y, z = arr
y == 1 # true
z == 2 # true
You can see from the following code that destructuring in arguments to blocks isn't unique to the built-in methods that take a block:
您可以从以下代码中看到,块的参数中的解构对于采用块的内置方法不是唯一的:
def my_method(arr)
yield arr
end
my_method([1, 2, 3]) {|x| puts x.inspect }
# => [1, 2, 3]
my_method([1, 2, 3]) {|x, y, z| puts x.inspect }
# => 1
Check out Destructuring with Ruby for more information.
有关更多信息,请查看使用Ruby进行解构。
#2
3
You can do some interesting restructuring in block parameters, depending on the structure of your array:
您可以在块参数中进行一些有趣的重组,具体取决于数组的结构:
[[1, 2], [3, 4], [5, 6], [7, 8]].reject {|x,y| y == 8 }
#=> [[1, 2], [3, 4], [5, 6]]
You can group them in parentheses:
您可以将它们分组在括号中:
[ [[1,2],3], [[1,3],6] ].select {|(x,y),z| x == 1 && z == 3 }
#=> [ [[1,2],3] ]
You can also use the splat operator for various things, like dealing with variable-length subarrays:
您还可以使用splat运算符执行各种操作,例如处理可变长度子数组:
[[:a,:b,2,3,4,5,6], [:c,:d,7,8,9]].each {|x,y,*numbers| puts numbers.inspect }
#=> [2,3,4,5,6]
#=> [7,8,9]
#3
1
Ruby is flexible in how it interprets the arguments; here is a similar example, with one and then two arguments:
Ruby在解释参数方面很灵活;这是一个类似的例子,有一个然后是两个参数:
[1, 3].product([2, 4]).reject {|a| a.first > a.last }
=> [[1, 2], [1, 4], [3, 4]]
[1, 3].product([2, 4]).reject {|a,b| a > b }
=> [[1, 2], [1, 4], [3, 4]]
The rule of thumb here is that you can treat the arguments either as a composite object, or as individual elements in a collection. E.g.,
这里的经验法则是,您可以将参数视为复合对象,也可以视为集合中的单个元素。例如。,
[1, 2, 3].tap {|a,b,c| puts [a,b,c].inspect }
[1, 2, 3]
...
[1, 2, 3].tap {|a,b| puts [a,b].inspect }
[1, 2]
...
[1, 2, 3].tap {|a| puts a.inspect }
[1, 2, 3]