backports 1.6.8 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,68 +9,80 @@ module Enumerable
9
9
  end
10
10
  end unless method_defined? :sum
11
11
 
12
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
13
- def count(*arg)
14
- result = 0
15
- if block_given?
16
- each{|o| result += 1 if yield o}
17
- elsif arg.empty?
18
- each{|o| result += 1}
12
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
13
+ def count(item = Undefined)
14
+ seq = 0
15
+ if item != Undefined
16
+ each { |o| seq += 1 if item == o }
17
+ elsif block_given?
18
+ each { |o| seq += 1 if yield(o) }
19
19
  else
20
- obj = arg.first
21
- each{|o| result += 1 if obj == o}
20
+ each { seq += 1 }
22
21
  end
23
- result
22
+ seq
24
23
  end unless method_defined? :count
25
24
 
26
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
27
- def cycle(*arg, &block)
28
- return to_enum(:cycle, *arg) unless block_given?
29
- to_a.cycle(*arg, &block)
25
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
26
+ def cycle(n = nil, &block)
27
+ return to_enum(:cycle, n) unless block_given?
28
+ return loop(&block) if nil == n
29
+ n = Type.coerce_to(n, Fixnum, :to_int)
30
+ if n >= 1
31
+ cache = []
32
+ each do |elem|
33
+ cache << elem
34
+ block.call(elem)
35
+ end
36
+ cache.cycle(n-1, &block)
37
+ end
30
38
  end unless method_defined? :cycle
31
39
 
32
- make_block_optional :detect, :find, :test_on => [42]
40
+ make_block_optional :detect, :find, :find_all, :select, :sort_by, :partition, :reject, :test_on => 1..2
33
41
 
34
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
42
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
35
43
  def drop(n)
36
- array = to_a
37
- array[n...array.size] || []
44
+ n = Type.coerce_to(n, Fixnum, :to_int)
45
+ raise ArgumentError, "attempt to drop negative size" if n < 0
46
+ ary = to_a
47
+ return [] if n > ary.size
48
+ ary[n...ary.size]
38
49
  end unless method_defined? :drop
39
50
 
40
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
51
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
41
52
  def drop_while(&block)
42
53
  return to_enum(:drop_while) unless block_given?
43
- array = to_a
44
- array.each_with_index do |element, i|
45
- return array.drop(i) unless yield(element)
54
+ ary = []
55
+ dropping = true
56
+ each do |obj|
57
+ ary << obj unless dropping &&= yield(obj)
46
58
  end
47
- []
59
+ ary
48
60
  end unless method_defined? :drop_while
49
61
 
50
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
62
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
51
63
  make_block_optional :each_cons, :each_slice, :test_on => 1..2, :arg => 1
52
64
 
53
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
65
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
54
66
  if instance_method(:each_with_index).arity.zero?
55
67
  def each_with_index_with_optional_args_and_block(*args, &block)
56
68
  return to_enum(:each_with_index, *args) unless block_given?
57
- to_enum(:each, *args).each_with_index_without_optional_args_and_block(&block)
69
+ idx = 0
70
+ each(*args) { |o| yield(o, idx); idx += 1 }
71
+ self
58
72
  end
59
73
  alias_method_chain :each_with_index, :optional_args_and_block
60
74
  end
61
75
 
62
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
76
+ # Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
63
77
  def each_with_object(memo, &block)
64
78
  return to_enum(:each_with_object, memo) unless block_given?
65
79
  each {|obj| block.call(obj, memo)}
66
80
  memo
67
81
  end unless method_defined? :each_with_object
68
82
 
69
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
70
- def find_index(*args)
71
- raise ArgumentError, "Wrong number of arguments (#{args.size} for 1)" if args.size > 1
72
- if args.size == 1
73
- obj = args.first
83
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
84
+ def find_index(obj = Undefined)
85
+ if obj != Undefined
74
86
  each_with_index do |element, i|
75
87
  return i if element == obj
76
88
  end
@@ -78,19 +90,20 @@ module Enumerable
78
90
  each_with_index do |element, i|
79
91
  return i if yield element
80
92
  end
81
- each_with_index{|o,i| return i if yield o}
82
93
  else
83
94
  return to_enum(:find_index)
84
95
  end
85
96
  nil
86
97
  end unless method_defined? :find_index
87
98
 
88
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
89
- def first(*arg)
90
- arg.empty? ? take(1)[0] : take(arg.first)
99
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
100
+ def first(n = Undefined)
101
+ return take(n) unless n == Undefined
102
+ each{|obj| return obj}
103
+ nil
91
104
  end unless method_defined? :first
92
105
 
93
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
106
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
94
107
  def group_by
95
108
  return to_enum(:group_by) unless block_given?
96
109
  returning({}) do |result|
@@ -100,13 +113,11 @@ module Enumerable
100
113
  end
101
114
  end unless method_defined? :group_by
102
115
 
103
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
116
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
104
117
  unless ((1..2).inject(:+) rescue false)
105
118
  def inject_with_symbol(*args, &block)
106
- return inject_without_symbol(*args, &block) if block_given?
119
+ return inject_without_symbol(*args, &block) if block_given? && args.size <= 1
107
120
  method = args.pop
108
- raise TypeError, "#{method} is not a symbol" unless method.respond_to? :to_sym
109
- method = method.to_sym
110
121
  inject_without_symbol(*args) {|memo, obj| memo.send(method, obj)}
111
122
  end
112
123
  alias_method_chain :inject, :symbol
@@ -123,19 +134,19 @@ module Enumerable
123
134
  end
124
135
  end
125
136
 
126
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
137
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
127
138
  def max_by(&block)
128
139
  return to_enum(:max_by) unless block_given?
129
140
  minmax_by(&block)[1]
130
141
  end unless method_defined? :max_by
131
142
 
132
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
143
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
133
144
  def min_by(&block)
134
145
  return to_enum(:min_by) unless block_given?
135
146
  minmax_by(&block).first
136
147
  end unless method_defined? :min_by
137
148
 
138
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
149
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
139
150
  def minmax
140
151
  return minmax{|a,b| a <=> b} unless block_given?
141
152
  first_time = true
@@ -145,14 +156,14 @@ module Enumerable
145
156
  min = max = object
146
157
  first_time = false
147
158
  else
148
- min = object if yield(min, object) > 0
149
- max = object if yield(max, object) < 0
159
+ min = object if Type.coerce_to_comparison(min, object, yield(min, object)) > 0
160
+ max = object if Type.coerce_to_comparison(max, object, yield(max, object)) < 0
150
161
  end
151
162
  end
152
163
  [min, max]
153
164
  end unless method_defined? :minmax
154
165
 
155
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
166
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
156
167
  def minmax_by(&block)
157
168
  return to_enum(:minmax_by) unless block_given?
158
169
  min_object, min_result = nil, MOST_EXTREME_OBJECT_EVER
@@ -165,20 +176,35 @@ module Enumerable
165
176
  [min_object, max_object]
166
177
  end unless method_defined? :minmax_by
167
178
 
168
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
179
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
169
180
  def none?(&block)
170
181
  !any?(&block)
171
182
  end unless method_defined? :none?
172
183
 
173
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
184
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
174
185
  def one?(&block)
175
- return one?{|o| o} unless block_given?
176
- 1 == count(&block)
186
+ found_one = false
187
+ if block_given?
188
+ each do |o|
189
+ if yield(o)
190
+ return false if found_one
191
+ found_one = true
192
+ end
193
+ end
194
+ else
195
+ each do |o|
196
+ if o
197
+ return false if found_one
198
+ found_one = true
199
+ end
200
+ end
201
+ end
202
+ found_one
177
203
  end unless method_defined? :one?
178
204
 
179
205
  alias_method :reduce, :inject unless method_defined? :reduce
180
206
 
181
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
207
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
182
208
  def reverse_each(&block)
183
209
  return to_enum(:reverse_each) unless block_given?
184
210
  # There is no other way then to convert to an array first... see 1.9's source.
@@ -186,17 +212,19 @@ module Enumerable
186
212
  self
187
213
  end unless method_defined? :reverse_each
188
214
 
189
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
215
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
190
216
  def take(n)
217
+ n = Type.coerce_to(n, Fixnum, :to_int)
218
+ raise ArgumentError, "attempt to take negative size: #{n}" if n < 0
191
219
  returning([]) do |array|
192
220
  each do |elem|
193
- array << elem
194
221
  break if array.size >= n
222
+ array << elem
195
223
  end unless n <= 0
196
224
  end
197
225
  end unless method_defined? :take
198
226
 
199
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
227
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
200
228
  def take_while
201
229
  return to_enum(:take_while) unless block_given?
202
230
  inject([]) do |array, elem|
@@ -205,7 +233,7 @@ module Enumerable
205
233
  end
206
234
  end unless method_defined? :take_while
207
235
 
208
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
236
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
209
237
  if instance_method(:to_a).arity.zero?
210
238
  def to_a_with_optional_arguments(*args)
211
239
  return to_a_without_optional_arguments if args.empty?
@@ -214,4 +242,13 @@ module Enumerable
214
242
  alias_method_chain :to_a, :optional_arguments
215
243
  end
216
244
 
245
+ # alias_method gives a warning, so instead copy-paste:
246
+ if instance_method(:entries).arity.zero?
247
+ def entries_with_optional_arguments(*args)
248
+ return entries_without_optional_arguments if args.empty?
249
+ to_enum(:each, *args).entries
250
+ end
251
+ alias_method_chain :entries, :optional_arguments
252
+ end
253
+
217
254
  end
@@ -1,38 +1,40 @@
1
1
  class Enumerator
2
2
  alias_method :with_object, :each_with_object unless method_defined? :with_object
3
-
3
+
4
4
  def next
5
5
  require 'generator'
6
6
  @generator ||= Generator.new(self)
7
7
  raise StopIteration unless @generator.next?
8
8
  @generator.next
9
9
  end unless method_defined? :next
10
-
10
+
11
11
  def rewind
12
+ @object.rewind if @object.respond_to? :rewind
12
13
  require 'generator'
13
14
  @generator ||= Generator.new(self)
14
15
  @generator.rewind
15
16
  self
16
17
  end unless method_defined? :rewind
17
-
18
+
18
19
  # new with block, standard in Ruby 1.9
19
20
  unless (self.new{} rescue false)
20
21
  class Yielder
21
22
  def initialize(&block)
22
23
  @main_block = block
23
24
  end
24
-
25
+
25
26
  def each(&block)
26
27
  @final_block = block
27
28
  @main_block.call(self)
28
29
  end
29
-
30
+
30
31
  def yield(*arg)
31
32
  @final_block.yield(*arg)
32
33
  end
33
34
  end
34
-
35
+
35
36
  def initialize_with_optional_block(*arg, &block)
37
+ @object = arg.first
36
38
  return initialize_without_optional_block(*arg, &nil) unless arg.empty? # Ruby 1.9 apparently ignores the block if any argument is present
37
39
  initialize_without_optional_block(Yielder.new(&block))
38
40
  end
@@ -0,0 +1,3 @@
1
+ class << ENV
2
+ make_block_optional :delete_if, :each, :each_key, :each_pair, :each_value, :reject!, :select, :test_on => ENV
3
+ end
@@ -1,7 +1,7 @@
1
1
  class Fixnum
2
2
  # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Fixnum.html]
3
3
  def div(n)
4
- (self / n).round
4
+ (self / n).to_i
5
5
  end unless method_defined? :div
6
6
 
7
7
  # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Fixnum.html]
@@ -0,0 +1,4 @@
1
+ class Fixnum
2
+ # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Fixnum.html]
3
+ alias_method :fdiv, :/ unless method_defined? :fdiv
4
+ end
@@ -0,0 +1,9 @@
1
+ module GC
2
+ def self.stress
3
+ false
4
+ end
5
+
6
+ def self.stress=(flag)
7
+ raise NotImplementedError
8
+ end
9
+ end
@@ -1,41 +1,47 @@
1
1
  class Hash
2
- # New ruby 1.9 constructor -- not documented?
2
+ # New Ruby 1.8.7+ constructor -- not documented, see redmine # 1385
3
3
  # <tt>Hash[[[:foo, :bar],[:hello, "world"]]] ==> {:foo => :bar, :hello => "world"}</tt>
4
4
  class << self
5
5
  alias_method :original_constructor, :[]
6
- def [](*arg)
7
- return original_constructor(*arg) unless arg.length == 1 && arg.first.is_a?(Array)
6
+ def [](*args)
7
+ return original_constructor(*args) unless args.length == 1 && args.first.is_a?(Array)
8
8
  returning({}) do |h|
9
- arg.first.each{|key, value| h[key] = value}
9
+ args.first.each do |arr|
10
+ next unless arr.respond_to? :to_ary
11
+ arr = arr.to_ary
12
+ next unless (1..2).include? arr.size
13
+ h[arr.at(0)] = arr.at(1)
14
+ end
10
15
  end
11
16
  end unless (Hash[[[:test, :test]]] rescue false)
12
-
17
+
18
+ # Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Hash.html]
13
19
  def try_convert(x)
14
20
  return nil unless x.respond_to? :to_hash
15
21
  x.to_hash
16
22
  end unless method_defined? :to_hash
17
23
  end
18
24
 
19
- make_block_optional :delete_if, :each, :each_key, :each_pair, :each_value, :reject!, :select, :test_on => {:hello => "world!"}
20
-
21
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Hash.html]
25
+ make_block_optional :delete_if, :each, :each_key, :each_pair, :each_value, :reject, :reject!, :select, :test_on => {:hello => "world!"}
26
+
27
+ # Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Hash.html]
22
28
  def default_proc=(proc)
23
- replace(Hash.new(&proc).merge!(self))
29
+ replace(Hash.new(&Type.coerce_to(proc, Proc, :to_proc)).merge!(self))
24
30
  end unless method_defined? :default_proc=
25
31
 
26
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Hash.html]
32
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Hash.html]
27
33
  alias_method :key, :index unless method_defined? :key
28
-
34
+
29
35
  # Standard in rails. See official documentation[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Hash/Keys.html]
30
36
  def reverse_merge(other_hash)
31
37
  other_hash.merge(self)
32
38
  end
33
-
39
+
34
40
  # Standard in rails. See official documentation[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Hash/Keys.html]
35
41
  def reverse_merge!(other_hash)
36
42
  replace(reverse_merge(other_hash))
37
43
  end
38
-
44
+
39
45
  # Standard in rails. See official documentation[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Hash/Keys.html]
40
46
  def symbolize_keys
41
47
  Hash[map{|key,value| [(key.to_sym rescue key) || key, value] }]
@@ -55,8 +61,4 @@ class Hash
55
61
  def stringify_keys!
56
62
  self.replace(self.stringify_keys)
57
63
  end unless method_defined? :stringify_keys!
58
- end
59
-
60
- class << ENV
61
- make_block_optional :delete_if, :each, :each_key, :each_pair, :each_value, :reject!, :select, :test_on => ENV
62
64
  end
@@ -12,23 +12,14 @@ class Integer
12
12
  # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Fixnum.html]
13
13
  def ord
14
14
  self
15
- end
16
-
17
- # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Fixnum.html]
18
- def succ
19
- self + 1
20
- end unless method_defined? :succ
15
+ end unless method_defined? :ord
21
16
 
22
17
  # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Fixnum.html]
23
18
  def pred
24
19
  self - 1
25
- end unless method_defined? :succ
26
-
20
+ end unless method_defined? :pred
21
+
27
22
  alias_method :magnitude, :abs unless method_defined? :magnitude
28
-
29
- make_block_optional :downto, :times, :upto, :test_on => 42, :arg => 42
30
- end
31
23
 
32
- class Numeric
33
- make_block_optional :step, :test_on => 42, :arg => [100, 6]
24
+ make_block_optional :downto, :times, :upto, :test_on => 42, :arg => 42
34
25
  end