backports 1.7.1 → 1.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +7 -0
- data/README.rdoc +31 -36
- data/Rakefile +4 -27
- data/VERSION.yml +2 -2
- data/backports.gemspec +52 -32
- data/lib/backports.rb +4 -15
- data/lib/backports/1.8.7.rb +4 -0
- data/lib/backports/{argf.rb → 1.8.7/argf.rb} +8 -8
- data/lib/backports/{array.rb → 1.8.7/array.rb} +26 -43
- data/lib/backports/{binding.rb → 1.8.7/binding.rb} +0 -0
- data/lib/backports/1.8.7/dir.rb +7 -0
- data/lib/backports/{enumerable.rb → 1.8.7/enumerable.rb} +22 -38
- data/lib/backports/1.8.7/enumerator.rb +19 -0
- data/lib/backports/1.8.7/env.rb +3 -0
- data/lib/backports/1.8.7/fixnum.rb +11 -0
- data/lib/backports/1.8.7/float.rb +4 -0
- data/lib/backports/{gc.rb → 1.8.7/gc.rb} +0 -0
- data/lib/backports/1.8.7/hash.rb +23 -0
- data/lib/backports/1.8.7/integer.rb +23 -0
- data/lib/backports/{io.rb → 1.8.7/io.rb} +8 -7
- data/lib/backports/1.8.7/kernel.rb +39 -0
- data/lib/backports/{method.rb → 1.8.7/method.rb} +8 -8
- data/lib/backports/1.8.7/module.rb +7 -0
- data/lib/backports/1.8.7/numeric.rb +3 -0
- data/lib/backports/1.8.7/object_space.rb +5 -0
- data/lib/backports/{proc.rb → 1.8.7/proc.rb} +0 -0
- data/lib/backports/{process.rb → 1.8.7/process.rb} +0 -0
- data/lib/backports/1.8.7/range.rb +4 -0
- data/lib/backports/{regexp.rb → 1.8.7/regexp.rb} +2 -1
- data/lib/backports/1.8.7/string.rb +90 -0
- data/lib/backports/1.8.7/struct.rb +3 -0
- data/lib/backports/1.8.7/symbol.rb +6 -0
- data/lib/backports/1.9.rb +5 -0
- data/lib/backports/1.9/array.rb +12 -0
- data/lib/backports/1.9/enumerable.rb +8 -0
- data/lib/backports/1.9/enumerator.rb +15 -0
- data/lib/backports/1.9/hash.rb +15 -0
- data/lib/backports/1.9/integer.rb +3 -0
- data/lib/backports/1.9/kernel.rb +11 -0
- data/lib/backports/1.9/string.rb +39 -0
- data/lib/backports/{symbol.rb → 1.9/symbol.rb} +1 -6
- data/lib/backports/rails.rb +4 -0
- data/lib/backports/rails/array.rb +6 -0
- data/lib/backports/rails/enumerable.rb +12 -0
- data/lib/backports/rails/hash.rb +31 -0
- data/lib/backports/rails/kernel.rb +12 -0
- data/lib/backports/rails/module.rb +6 -0
- data/lib/backports/rails/string.rb +42 -0
- data/lib/backports/tools.rb +117 -0
- data/test/enumerator_test.rb +3 -3
- data/test/test_helper.rb +1 -3
- metadata +50 -31
- data/lib/backports/core_ext.rb +0 -61
- data/lib/backports/dir.rb +0 -7
- data/lib/backports/enumerator.rb +0 -43
- data/lib/backports/env.rb +0 -3
- data/lib/backports/fixnum.rb +0 -11
- data/lib/backports/float.rb +0 -4
- data/lib/backports/hash.rb +0 -64
- data/lib/backports/integer.rb +0 -25
- data/lib/backports/kernel.rb +0 -55
- data/lib/backports/module.rb +0 -30
- data/lib/backports/numeric.rb +0 -3
- data/lib/backports/object_space.rb +0 -5
- data/lib/backports/range.rb +0 -4
- data/lib/backports/string.rb +0 -167
- data/lib/backports/struct.rb +0 -3
File without changes
|
@@ -1,18 +1,9 @@
|
|
1
|
+
require 'enumerator'
|
1
2
|
module Enumerable
|
2
|
-
# Standard in rails... See official documentation[http://api.rubyonrails.org/classes/Enumerable.html]
|
3
|
-
# Modified from rails 2.3 to not rely on size
|
4
|
-
def sum(identity = 0, &block)
|
5
|
-
if block_given?
|
6
|
-
map(&block).sum(identity)
|
7
|
-
else
|
8
|
-
inject { |sum, element| sum + element } || identity
|
9
|
-
end
|
10
|
-
end unless method_defined? :sum
|
11
|
-
|
12
3
|
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
|
13
|
-
def count(item = Undefined)
|
4
|
+
def count(item = Backports::Undefined)
|
14
5
|
seq = 0
|
15
|
-
if item != Undefined
|
6
|
+
if item != Backports::Undefined
|
16
7
|
each { |o| seq += 1 if item == o }
|
17
8
|
elsif block_given?
|
18
9
|
each { |o| seq += 1 if yield(o) }
|
@@ -26,7 +17,7 @@ module Enumerable
|
|
26
17
|
def cycle(n = nil, &block)
|
27
18
|
return to_enum(:cycle, n) unless block_given?
|
28
19
|
return loop(&block) if nil == n
|
29
|
-
n =
|
20
|
+
n = Backports.coerce_to(n, Fixnum, :to_int)
|
30
21
|
if n >= 1
|
31
22
|
cache = []
|
32
23
|
each do |elem|
|
@@ -37,11 +28,14 @@ module Enumerable
|
|
37
28
|
end
|
38
29
|
end unless method_defined? :cycle
|
39
30
|
|
40
|
-
|
31
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
|
32
|
+
Backports.make_block_optional self, :each_cons, :each_slice, :test_on => 1..2, :arg => 1
|
33
|
+
|
34
|
+
Backports.make_block_optional self, :detect, :find, :find_all, :select, :sort_by, :partition, :reject, :test_on => 1..2
|
41
35
|
|
42
36
|
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
|
43
37
|
def drop(n)
|
44
|
-
n =
|
38
|
+
n = Backports.coerce_to(n, Fixnum, :to_int)
|
45
39
|
raise ArgumentError, "attempt to drop negative size" if n < 0
|
46
40
|
ary = to_a
|
47
41
|
return [] if n > ary.size
|
@@ -59,9 +53,6 @@ module Enumerable
|
|
59
53
|
ary
|
60
54
|
end unless method_defined? :drop_while
|
61
55
|
|
62
|
-
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
|
63
|
-
make_block_optional :each_cons, :each_slice, :test_on => 1..2, :arg => 1
|
64
|
-
|
65
56
|
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
|
66
57
|
if instance_method(:each_with_index).arity.zero?
|
67
58
|
def each_with_index_with_optional_args_and_block(*args, &block)
|
@@ -70,19 +61,12 @@ module Enumerable
|
|
70
61
|
each(*args) { |o| yield(o, idx); idx += 1 }
|
71
62
|
self
|
72
63
|
end
|
73
|
-
alias_method_chain :each_with_index, :optional_args_and_block
|
64
|
+
Backports.alias_method_chain self, :each_with_index, :optional_args_and_block
|
74
65
|
end
|
75
|
-
|
76
|
-
# Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
|
77
|
-
def each_with_object(memo, &block)
|
78
|
-
return to_enum(:each_with_object, memo) unless block_given?
|
79
|
-
each {|obj| block.call(obj, memo)}
|
80
|
-
memo
|
81
|
-
end unless method_defined? :each_with_object
|
82
66
|
|
83
67
|
# 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
|
68
|
+
def find_index(obj = Backports::Undefined)
|
69
|
+
if obj != Backports::Undefined
|
86
70
|
each_with_index do |element, i|
|
87
71
|
return i if element == obj
|
88
72
|
end
|
@@ -97,8 +81,8 @@ module Enumerable
|
|
97
81
|
end unless method_defined? :find_index
|
98
82
|
|
99
83
|
# 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
|
84
|
+
def first(n = Backports::Undefined)
|
85
|
+
return take(n) unless n == Backports::Undefined
|
102
86
|
each{|obj| return obj}
|
103
87
|
nil
|
104
88
|
end unless method_defined? :first
|
@@ -106,7 +90,7 @@ module Enumerable
|
|
106
90
|
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
|
107
91
|
def group_by
|
108
92
|
return to_enum(:group_by) unless block_given?
|
109
|
-
|
93
|
+
{}.tap do |result|
|
110
94
|
each do |o|
|
111
95
|
result.fetch(yield(o)){|key| result[key] = []} << o
|
112
96
|
end
|
@@ -120,7 +104,7 @@ module Enumerable
|
|
120
104
|
method = args.pop
|
121
105
|
inject_without_symbol(*args) {|memo, obj| memo.send(method, obj)}
|
122
106
|
end
|
123
|
-
alias_method_chain :inject, :symbol
|
107
|
+
Backports.alias_method_chain self, :inject, :symbol
|
124
108
|
end
|
125
109
|
|
126
110
|
MOST_EXTREME_OBJECT_EVER = Object.new # :nodoc:
|
@@ -156,8 +140,8 @@ module Enumerable
|
|
156
140
|
min = max = object
|
157
141
|
first_time = false
|
158
142
|
else
|
159
|
-
min = object if
|
160
|
-
max = object if
|
143
|
+
min = object if Backports.coerce_to_comparison(min, object, yield(min, object)) > 0
|
144
|
+
max = object if Backports.coerce_to_comparison(max, object, yield(max, object)) < 0
|
161
145
|
end
|
162
146
|
end
|
163
147
|
[min, max]
|
@@ -214,9 +198,9 @@ module Enumerable
|
|
214
198
|
|
215
199
|
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
|
216
200
|
def take(n)
|
217
|
-
n =
|
201
|
+
n = Backports.coerce_to(n, Fixnum, :to_int)
|
218
202
|
raise ArgumentError, "attempt to take negative size: #{n}" if n < 0
|
219
|
-
|
203
|
+
[].tap do |array|
|
220
204
|
each do |elem|
|
221
205
|
array << elem
|
222
206
|
break if array.size >= n
|
@@ -239,7 +223,7 @@ module Enumerable
|
|
239
223
|
return to_a_without_optional_arguments if args.empty?
|
240
224
|
to_enum(:each, *args).to_a
|
241
225
|
end
|
242
|
-
alias_method_chain :to_a, :optional_arguments
|
226
|
+
Backports.alias_method_chain self, :to_a, :optional_arguments
|
243
227
|
end
|
244
228
|
|
245
229
|
# alias_method gives a warning, so instead copy-paste:
|
@@ -248,7 +232,7 @@ module Enumerable
|
|
248
232
|
return entries_without_optional_arguments if args.empty?
|
249
233
|
to_enum(:each, *args).entries
|
250
234
|
end
|
251
|
-
alias_method_chain :entries, :optional_arguments
|
235
|
+
Backports.alias_method_chain self, :entries, :optional_arguments
|
252
236
|
end
|
253
237
|
|
254
238
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'enumerator'
|
2
|
+
module Enumerable
|
3
|
+
class Enumerator
|
4
|
+
def next
|
5
|
+
require 'generator'
|
6
|
+
@generator ||= Generator.new(self)
|
7
|
+
raise StopIteration unless @generator.next?
|
8
|
+
@generator.next
|
9
|
+
end unless method_defined? :next
|
10
|
+
|
11
|
+
def rewind
|
12
|
+
@object.rewind if @object.respond_to? :rewind
|
13
|
+
require 'generator'
|
14
|
+
@generator ||= Generator.new(self)
|
15
|
+
@generator.rewind
|
16
|
+
self
|
17
|
+
end unless method_defined? :rewind
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class Fixnum
|
2
|
+
# Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Fixnum.html]
|
3
|
+
def div(n)
|
4
|
+
(self / n).to_i
|
5
|
+
end unless method_defined? :div
|
6
|
+
|
7
|
+
# Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Fixnum.html]
|
8
|
+
def fdiv(n)
|
9
|
+
to_f / n
|
10
|
+
end unless method_defined? :fdiv
|
11
|
+
end
|
File without changes
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class Hash
|
2
|
+
# New Ruby 1.8.7+ constructor -- not documented, see redmine # 1385
|
3
|
+
# <tt>Hash[[[:foo, :bar],[:hello, "world"]]] ==> {:foo => :bar, :hello => "world"}</tt>
|
4
|
+
class << self
|
5
|
+
alias_method :original_constructor, :[]
|
6
|
+
def [](*args)
|
7
|
+
return original_constructor(*args) unless args.length == 1 && args.first.is_a?(Array)
|
8
|
+
{}.tap do |h|
|
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
|
15
|
+
end
|
16
|
+
end unless (Hash[[[:test, :test]]] rescue false)
|
17
|
+
end
|
18
|
+
|
19
|
+
Backports.make_block_optional self, :delete_if, :each, :each_key, :each_pair, :each_value, :reject, :reject!, :select, :test_on => {:hello => "world!"}
|
20
|
+
|
21
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Hash.html]
|
22
|
+
alias_method :key, :index unless method_defined? :key
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class Integer
|
2
|
+
Backports.make_block_optional self, :downto, :times, :upto, :test_on => 42, :arg => 42
|
3
|
+
|
4
|
+
# Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Fixnum.html]
|
5
|
+
def even?
|
6
|
+
self[0].zero?
|
7
|
+
end unless method_defined? :even?
|
8
|
+
|
9
|
+
# Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Fixnum.html]
|
10
|
+
def odd?
|
11
|
+
!even?
|
12
|
+
end unless method_defined? :odd?
|
13
|
+
|
14
|
+
# Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Fixnum.html]
|
15
|
+
def ord
|
16
|
+
self
|
17
|
+
end unless method_defined? :ord
|
18
|
+
|
19
|
+
# Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Fixnum.html]
|
20
|
+
def pred
|
21
|
+
self - 1
|
22
|
+
end unless method_defined? :pred
|
23
|
+
end
|
@@ -1,22 +1,22 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
make_block_optional :each, :each_line, :each_byte
|
1
|
+
class IO
|
2
|
+
if RUBY_VERSION < '1.8.7'
|
3
|
+
Backports.make_block_optional self, :each, :each_line, :each_byte
|
4
4
|
class << self
|
5
|
-
make_block_optional :foreach
|
5
|
+
Backports.make_block_optional self, :foreach
|
6
6
|
end
|
7
7
|
end
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
class IO
|
9
|
+
# Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/IO.html]
|
12
10
|
def bytes
|
13
11
|
to_enum :each_byte
|
14
12
|
end unless method_defined? :bytes
|
15
13
|
|
14
|
+
# Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/IO.html]
|
16
15
|
def chars
|
17
16
|
to_enum :each_char
|
18
17
|
end unless method_defined? :chars
|
19
18
|
|
19
|
+
# Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/IO.html]
|
20
20
|
def each_char
|
21
21
|
return to_enum(:each_char) unless block_given?
|
22
22
|
if $KCODE == "UTF-8"
|
@@ -47,6 +47,7 @@ class IO
|
|
47
47
|
alias_method :getbyte, :getc unless method_defined? :getbyte
|
48
48
|
alias_method :readbyte, :readchar unless method_defined? :readchar
|
49
49
|
|
50
|
+
# Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/IO.html]
|
50
51
|
def lines(*args)
|
51
52
|
to_enum :each_line, *args
|
52
53
|
end unless method_defined? :lines
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Kernel
|
2
|
+
|
3
|
+
def __method__
|
4
|
+
m = caller(1).first[/`(.*)'/,1]
|
5
|
+
m.to_sym if m
|
6
|
+
end unless (__method__ || true rescue false)
|
7
|
+
|
8
|
+
# Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Object.html]
|
9
|
+
def define_singleton_method(*args, &block)
|
10
|
+
class << self
|
11
|
+
self
|
12
|
+
end.send(:define_method, *args, &block)
|
13
|
+
end unless method_defined? :define_singleton_method
|
14
|
+
|
15
|
+
# Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Object.html]
|
16
|
+
def instance_exec(*arg, &block)
|
17
|
+
define_singleton_method(:"temporary method for instance_exec", &block)
|
18
|
+
send(:"temporary method for instance_exec", *arg)
|
19
|
+
end unless method_defined? :instance_exec
|
20
|
+
|
21
|
+
# Loop. Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Object.html]
|
22
|
+
unless const_defined? :StopIteration
|
23
|
+
class StopIteration < IndexError; end
|
24
|
+
|
25
|
+
def loop_with_stop_iteration(&block)
|
26
|
+
loop_without_stop_iteration(&block)
|
27
|
+
rescue StopIteration
|
28
|
+
# ignore silently
|
29
|
+
end
|
30
|
+
Backports.alias_method_chain self, :loop, :stop_iteration
|
31
|
+
end
|
32
|
+
|
33
|
+
# Standard in ruby 1.8.7. See official documentation[http://ruby-doc.org/core-1.9/classes/Object.html]
|
34
|
+
def tap
|
35
|
+
yield self
|
36
|
+
self
|
37
|
+
end unless method_defined? :tap
|
38
|
+
|
39
|
+
end
|
@@ -3,45 +3,45 @@ unless Method.method_defined? :name
|
|
3
3
|
attr_accessor :name, :receiver, :owner
|
4
4
|
|
5
5
|
def unbind_with_additional_info
|
6
|
-
|
6
|
+
unbind_without_additional_info.tap do |unbound|
|
7
7
|
unbound.name = name
|
8
8
|
unbound.owner = owner
|
9
9
|
end
|
10
10
|
end
|
11
|
-
alias_method_chain :unbind, :additional_info
|
11
|
+
Backports.alias_method_chain self, :unbind, :additional_info
|
12
12
|
end
|
13
13
|
|
14
14
|
class UnboundMethod
|
15
15
|
attr_accessor :name, :owner
|
16
16
|
|
17
17
|
def bind_with_additional_info(to)
|
18
|
-
|
18
|
+
bind_without_additional_info(to).tap do |bound|
|
19
19
|
bound.name = name
|
20
20
|
bound.owner = owner
|
21
21
|
bound.receiver = to
|
22
22
|
end
|
23
23
|
end
|
24
|
-
alias_method_chain :bind, :additional_info
|
24
|
+
Backports.alias_method_chain self, :bind, :additional_info
|
25
25
|
end
|
26
26
|
|
27
27
|
class Object
|
28
28
|
def method_with_additional_info(name)
|
29
|
-
|
29
|
+
method_without_additional_info(name).tap do |bound|
|
30
30
|
bound.name = name.to_sym
|
31
31
|
bound.receiver = self
|
32
32
|
bound.owner = self.class.ancestors.find{|mod| mod.instance_methods(false).include? name.to_s}
|
33
33
|
end
|
34
34
|
end
|
35
|
-
alias_method_chain :method, :additional_info
|
35
|
+
Backports.alias_method_chain self, :method, :additional_info
|
36
36
|
end
|
37
37
|
|
38
38
|
class Module
|
39
39
|
def instance_method_with_additional_info(name)
|
40
|
-
|
40
|
+
instance_method_without_additional_info(name).tap do |unbound|
|
41
41
|
unbound.name = name.to_sym
|
42
42
|
unbound.owner = ancestors.find{|mod| mod.instance_methods(false).include? name.to_s}
|
43
43
|
end
|
44
44
|
end
|
45
|
-
alias_method_chain :instance_method, :additional_info
|
45
|
+
Backports.alias_method_chain self, :instance_method, :additional_info
|
46
46
|
end
|
47
47
|
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
class Module
|
2
|
+
# Can't use alias_method here because of jruby (see http://jira.codehaus.org/browse/JRUBY-2435 )
|
3
|
+
def module_exec(*arg, &block)
|
4
|
+
instance_exec(*arg, &block)
|
5
|
+
end unless method_defined? :module_exec
|
6
|
+
alias_method :class_exec, :module_exec unless method_defined? :class_exec
|
7
|
+
end
|
File without changes
|
File without changes
|
@@ -1,11 +1,12 @@
|
|
1
1
|
class Regexp
|
2
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://www.ruby-doc.org/core-1.8.7/classes/Regexp.html]
|
2
3
|
class << self
|
3
4
|
unless (union(%w(a b)) rescue false)
|
4
5
|
def union_with_array_argument(*arg)
|
5
6
|
return union_without_array_argument(*arg) unless arg.size == 1
|
6
7
|
union_without_array_argument(*arg.first)
|
7
8
|
end
|
8
|
-
alias_method_chain :union, :array_argument
|
9
|
+
Backports.alias_method_chain self, :union, :array_argument
|
9
10
|
end
|
10
11
|
end
|
11
12
|
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
class String
|
2
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
3
|
+
alias_method :bytesize, :length unless method_defined? :bytesize
|
4
|
+
|
5
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
6
|
+
def chr
|
7
|
+
chars.first
|
8
|
+
end unless method_defined? :chr
|
9
|
+
|
10
|
+
Backports.make_block_optional self, :each_byte, :each, :each_line, :test_on => "abc"
|
11
|
+
|
12
|
+
# gsub: Left alone because of $~, $1, etc... which needs to be "pushed" up one level
|
13
|
+
# It's possible to do so, but gsub is used everywhere so i felt
|
14
|
+
# the performance hit was too big compared to the dubious gain.
|
15
|
+
|
16
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
17
|
+
unless method_defined? :each_char
|
18
|
+
def each_char(&block)
|
19
|
+
return to_enum(:each_char) unless block_given?
|
20
|
+
scan(/./, &block)
|
21
|
+
end
|
22
|
+
|
23
|
+
alias_method :chars, :each_char unless method_defined? :chars
|
24
|
+
end
|
25
|
+
|
26
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
27
|
+
def end_with?(*suffixes)
|
28
|
+
suffixes.each do |suffix|
|
29
|
+
next unless suffix.respond_to? :to_str
|
30
|
+
suffix = suffix.to_str
|
31
|
+
return true if self[-suffix.length, suffix.length] == suffix
|
32
|
+
end
|
33
|
+
false
|
34
|
+
end unless method_defined? :end_with?
|
35
|
+
|
36
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
37
|
+
unless ("check partition".partition(" ") rescue false)
|
38
|
+
def partition_with_new_meaning(pattern = Backports::Undefined, &block)
|
39
|
+
return partition_without_new_meaning(&block) if pattern == Backports::Undefined
|
40
|
+
pattern = Backports.coerce_to(pattern, String, :to_str) unless pattern.is_a? Regexp
|
41
|
+
i = index(pattern)
|
42
|
+
return [self, "", ""] unless i
|
43
|
+
if pattern.is_a? Regexp
|
44
|
+
match = Regexp.last_match
|
45
|
+
[match.pre_match, match[0], match.post_match]
|
46
|
+
else
|
47
|
+
last = i+pattern.length
|
48
|
+
[self[0...i], self[i...last], self[last...length]]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
Backports.alias_method_chain self, :partition, :new_meaning
|
52
|
+
end
|
53
|
+
|
54
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
55
|
+
def rpartition(pattern)
|
56
|
+
pattern = Backports.coerce_to(pattern, String, :to_str) unless pattern.is_a? Regexp
|
57
|
+
i = rindex(pattern)
|
58
|
+
return ["", "", self] unless i
|
59
|
+
|
60
|
+
if pattern.is_a? Regexp
|
61
|
+
match = Regexp.last_match
|
62
|
+
[match.pre_match, match[0], match.post_match]
|
63
|
+
else
|
64
|
+
last = i+pattern.length
|
65
|
+
[self[0...i], self[i...last], self[last...length]]
|
66
|
+
end
|
67
|
+
end unless method_defined? :rpartition
|
68
|
+
|
69
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
70
|
+
def start_with?(*prefixes)
|
71
|
+
prefixes.each do |prefix|
|
72
|
+
next unless prefix.respond_to? :to_str
|
73
|
+
prefix = prefix.to_str
|
74
|
+
return true if self[0, prefix.length] == prefix
|
75
|
+
end
|
76
|
+
false
|
77
|
+
end unless method_defined? :start_with?
|
78
|
+
|
79
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
80
|
+
unless ("abc".upto("def", true) rescue false)
|
81
|
+
def upto_with_exclusive(to, excl=false, &block)
|
82
|
+
return upto_without_exclusive(to, &block) if block_given? && !excl
|
83
|
+
enum = Range.new(self, to, excl).to_enum
|
84
|
+
return enum unless block_given?
|
85
|
+
enum.each(&block)
|
86
|
+
self
|
87
|
+
end
|
88
|
+
Backports.alias_method_chain self, :upto, :exclusive
|
89
|
+
end
|
90
|
+
end
|