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
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
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.
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.