backports 1.12.3 → 1.13.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +2 -2
- data/backports.gemspec +2 -2
- data/lib/backports.rb +0 -1
- data/lib/backports/1.8.7/array.rb +45 -15
- metadata +2 -2
data/VERSION.yml
CHANGED
data/backports.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{backports}
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.13.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Marc-Andr\303\251 Lafortune"]
|
12
|
-
s.date = %q{2010-01-
|
12
|
+
s.date = %q{2010-01-20}
|
13
13
|
s.description = %q{ Essential backports that enable some of the really nice features of ruby 1.8.7, ruby 1.9 and rails from ruby 1.8.6 and earlier.
|
14
14
|
}
|
15
15
|
s.email = %q{github@marc-andre.ca}
|
data/lib/backports.rb
CHANGED
@@ -90,6 +90,31 @@ class Array
|
|
90
90
|
alias_method :find_index, :index
|
91
91
|
end
|
92
92
|
|
93
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]
|
94
|
+
def permutation(num = Backports::Undefined, &block)
|
95
|
+
return to_enum(:permutation, num) unless block_given?
|
96
|
+
num = num.equal?(Backports::Undefined) ?
|
97
|
+
size :
|
98
|
+
Backports.coerce_to(num, Fixnum, :to_int)
|
99
|
+
return self unless (0..size).include? num
|
100
|
+
|
101
|
+
final_lambda = lambda do |partial, remain|
|
102
|
+
yield partial
|
103
|
+
end
|
104
|
+
|
105
|
+
outer_lambda = num.times.inject(final_lambda) do |proc, ignore|
|
106
|
+
lambda do |partial, remain|
|
107
|
+
remain.each_with_index do |val, i|
|
108
|
+
new_remain = remain.dup
|
109
|
+
new_remain.delete_at(i)
|
110
|
+
proc.call(partial.dup << val, new_remain)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
outer_lambda.call([], self)
|
116
|
+
end unless method_defined? :permutation
|
117
|
+
|
93
118
|
# pop. Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]
|
94
119
|
unless ([1].pop(1) rescue false)
|
95
120
|
def pop_with_optional_argument(n = Backports::Undefined)
|
@@ -105,22 +130,27 @@ class Array
|
|
105
130
|
|
106
131
|
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]
|
107
132
|
def product(*arg)
|
108
|
-
# Implementation notes: We build
|
109
|
-
# by building it up successively using "inject" and starting
|
110
|
-
#
|
111
|
-
# simply returns an array, so you'll find a simple call to "to_a" at the end.
|
133
|
+
# Implementation notes: We build a block that will generate all the combinations
|
134
|
+
# by building it up successively using "inject" and starting with one
|
135
|
+
# responsible to append the values.
|
112
136
|
#
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
137
|
+
result = []
|
138
|
+
|
139
|
+
arg.map!{|x| Type.coerce_to(x, Array, :to_ary)}
|
140
|
+
arg.reverse! # to get the results in the same order as in MRI, vary the last argument first
|
141
|
+
arg.push self
|
142
|
+
|
143
|
+
outer_lambda = arg.inject(result.method(:push)) do |proc, values|
|
144
|
+
lambda do |partial|
|
145
|
+
values.each do |val|
|
146
|
+
proc.call(partial.dup << val)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
outer_lambda.call([])
|
152
|
+
|
153
|
+
result
|
124
154
|
end unless method_defined? :product
|
125
155
|
|
126
156
|
# rindex. Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: backports
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.13.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "Marc-Andr\xC3\xA9 Lafortune"
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-01-
|
12
|
+
date: 2010-01-20 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|