将 proc 作为块提供给方法
假设我有以下数组:
arr = [[5, 1], [2, 7]]
并且我想找到最小元素,比较元素的第二个元素。最小元素将为 [5, 1]
,因为 1
小于 7
。我可以使用以下代码:
arr.min {|a,b| a[1] <=> b[1]}
为了计算最大值,我可以执行相同的操作:
arr.max {|a,b| a[1] <=> b[1]}
这给出了 [2, 7]
。
我一直使用同一个块。我想将该块放在某处并将其提供给最小/最大函数。我希望这样的事情:
blo = lambda {|a,b| a[1] <=> b[1]}
arr.min blo
会起作用,但没有。我知道如何做到这一点吗?
Let's say I have the following array:
arr = [[5, 1], [2, 7]]
and I want to find the minimum element, comparing the second element of the elements. The minimum element will be [5, 1]
since 1
is less than 7
. I can use the following code:
arr.min {|a,b| a[1] <=> b[1]}
For calculating the maximum, I can do the same:
arr.max {|a,b| a[1] <=> b[1]}
That gives [2, 7]
.
I use the same block all the time. I would like to have that block somewhere and provide it to the min/max function. I hoped something like:
blo = lambda {|a,b| a[1] <=> b[1]}
arr.min blo
would work, but it didn't. Any idea on how I can do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这个怎么样?
how about this?
解决此类问题的更通用的解决方案是完全避免嵌套数组并使用类。然后您可以定义 <=>;该类的运算符,使您可以访问 Comparable mixin (http://ruby-doc.org/core/classes/Comparable.html) 中的所有函数,为您提供 <、<=、==、>; =,并且>运算符和方法“之间?”
这只是一个示例,在现实生活中,您将使用描述其存储内容的类:
如果您有一个 Duo 对象数组,则可以使用 min、max 和 sort 函数,而无需定义比较运算符。所以...
会返回数组 [ @c, @b, @a ]
并且
会返回
@a
这比具有依赖于位置的逻辑的嵌套数据结构更像是“Ruby Way”在数组中。一开始需要做更多的工作,但从长远来看,你会发现它更好。
Ruby 是一种非常面向对象的编程语言,并提供了非常强大的工具供您使用。我强烈建议您阅读《Ruby 编程语言》或《The Ruby Way》之类的书,以正确了解该语言的强大功能。
A more general solution to problems like this is to avoid nested arrays entirely and use a class instead. You can then define the <=> operator for that class, giving you access to all the functions in the Comparable mixin (http://ruby-doc.org/core/classes/Comparable.html) gives you the <, <=, ==, >=, and > operators and the method 'between?'
This is just an example, in real life you would use classes that describe what they store:
If you have an array of Duo object you can then use the min, max, and sort functions without having to define the comparison operator. So...
would return the array [ @c, @b, @a ]
And
would return
@a
This is much more the 'Ruby Way' than nested data-structures with logic that relies on positions in arrays. It takes slightly more work at the start, but you'll find it much better in the long run.
Ruby is a very object oriented programming language and provides very powerful tools for you to use. I thoroughly recommend reading a book like "The Ruby Programming Language" or "The Ruby Way" to get a proper overview of the power of the language.
使用
&
运算符将Proc
对象转换为块。Use the
&
operator to turn aProc
object into a block.@sepp2k 的答案是更一般的答案,但在您的具体情况下,我只会使用,
因为这比所有那些大括号和方括号以及浮动的数组索引更明显。
@sepp2k's answer is the more general one, but in your specific case, I would just use
since that is much more obvious than all those curly braces and square brackets and array indices floating around.
如果您需要的只是最小值和最大值,您可以使用
Enumerable#minmax
方法并同时计算两者:编辑:天啊,我刚刚注意到还有
minmax_by
,所以你可以将它与last
方法结合起来,并具有:If all that you need is minimum and maximum, you might use
Enumerable#minmax
method and calculate both at once:Edit: Hell, I just noticed there is also
minmax_by
, so you can combine it withlast
method, and have: