tags:

views:

210

answers:

2

Is there a gem or a library to get ruby 1.9 methods like

[1, 2, 3].combination(2)
[1, 2, 3].permutation(2)
[1, 2, 3].product([1, 2, 3])
[1, 2, 3, 4, 5].cycle
+3  A: 

You could try the 1.8.x versions of Ruby Facets (http://facets.rubyforge.org/). Facets has become a bit of a mess (note 404s on website), but I have an old version of the gem installed (1.8.54) which has some of these pre-standard changes.

> gem install --version=1.8.54 facets

And then:

gem 'facets', "~>1.8"
require 'enumerator'
require 'facets/core/enumerable/cartesian_product'
require 'facets/core/enumerable/permutation'
require 'facets/core/enumerable/each_combination'

[1, 2, 3].enum_for(:each_combination,2).to_a   # note - only each form is available
[1, 2, 3].permutation(2)
[1, 2, 3].cartesian_product([1, 2, 3])         # note - rename
# Can't find .cycle equivalent after a quick search, maybe nothing there

You may want to alias some of these methods to get code compatibility.

Sorry, its not great.

Greg
I believe these have been renamed and moved in more recent versions. To avoid such compatibility problems, use `backports` instead (see my answer)
Marc-André Lafortune
+5  A: 

Sorry I missed this question. My gem backports implements in Ruby all the new features of Ruby 1.8.7, those of 1.8.8 (upcoming) and most of Ruby 1.9.x's. This of course includes #combination, #permutation, #product and #cycle.

The implementation in backports pass RubySpec (which is not the case for facets) so you are pretty much guaranteed never having any compatibility problem.

Marc-André Lafortune
thank you for answer and hard work :)
tig