阵列#uniq的与块相当于用Ruby 1.8.7 [英] Array#uniq with block equivalent in Ruby 1.8.7
问题描述
阵列#uniq的有这种行为在Ruby中1.9
Array#uniq has this behaviour in Ruby 1.9
c = [ "a:def", "a:xyz", "b:abc", "b:xyz", "c:jkl" ]
c.uniq {|s| s[/^\w+/]} #=> [ "a:def", "b:abc", "c:jkl" ]
它可以采取一个块,并给予我们给予什么方面的独特价值。但是,这不会用Ruby 1.8的工作。我怎么能在Ruby中创建1.8此功能?
It can take a block and give unique value with respect to what we give. But, this wont work in Ruby 1.8. How can I create this functionality in ruby 1.8?
推荐答案
马克 - 安德烈·LaFortune的 backports中的宝石:
Install Marc-André LaFortune's backports gem:
https://github.com/marcandre/backports
这有块版本1.9.2的阵列#uniq的
和阵列#uniq的!
。或者,如果你不想要或需要整个事情中,部分是pretty以及孤立的,所以你可以拉出正是您所需要的作品:
That has the block versions of 1.9.2's Array#uniq
and Array#uniq!
. Or if you don't want or need the whole thing, the parts are pretty well isolated so you can pull out just the pieces you need:
的https://github.com/marcandre/backports/blob/master/lib/backports/1.9.2/array.rb#L99
这篇关于阵列#uniq的与块相当于用Ruby 1.8.7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!