ruby 1.9 methods in ruby 1.8.6

帅比萌擦擦* 提交于 2019-12-24 01:57:10

问题


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

回答1:


This is exactly the goal of my gem backports.

It implements in pure Ruby all the new features of Ruby 1.8.7 and many of Ruby 1.9.x and 2.0. This of course includes #combination, #permutation, #product and #cycle.

You can, for example:

require 'backports/1.8.7/array/combination'
[1, 2, 3].combination(2) # => works, even in Ruby 1.8.6

The implementation in backports pass most RubySpecs (which is not the case for facets) to guarantee not having compatibility problems.




回答2:


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.



来源:https://stackoverflow.com/questions/1150359/ruby-1-9-methods-in-ruby-1-8-6

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!