marcandre-backports 1.6.8 → 1.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +7 -0
- data/CHANGELOG.rdoc +8 -0
- data/VERSION.yml +2 -2
- data/backports.gemspec +98 -0
- data/lib/backports/argf.rb +57 -0
- data/lib/backports/array.rb +92 -59
- data/lib/backports/binding.rb +1 -1
- data/lib/backports/core_ext.rb +61 -0
- data/lib/backports/enumerable.rb +93 -56
- data/lib/backports/enumerator.rb +8 -6
- data/lib/backports/env.rb +3 -0
- data/lib/backports/fixnum.rb +1 -1
- data/lib/backports/float.rb +4 -0
- data/lib/backports/gc.rb +9 -0
- data/lib/backports/hash.rb +19 -17
- data/lib/backports/integer.rb +4 -13
- data/lib/backports/io.rb +44 -2
- data/lib/backports/kernel.rb +43 -8
- data/lib/backports/module.rb +0 -20
- data/lib/backports/numeric.rb +3 -0
- data/lib/backports/object_space.rb +0 -1
- data/lib/backports/proc.rb +1 -1
- data/lib/backports/process.rb +5 -0
- data/lib/backports/string.rb +26 -26
- data/lib/backports/symbol.rb +1 -1
- data/lib/backports.rb +1 -8
- data/test/enumerable_test.rb +1 -1
- data/test/enumerator_test.rb +9 -0
- data/test/kernel_test.rb +6 -5
- metadata +12 -3
- data/lib/backports/object.rb +0 -37
data/lib/backports/string.rb
CHANGED
@@ -1,16 +1,18 @@
|
|
1
1
|
class String
|
2
2
|
class << self
|
3
|
-
# Standard in
|
3
|
+
# Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
4
4
|
def try_convert(x)
|
5
5
|
return nil unless x.respond_do(:to_str)
|
6
6
|
x.to_str
|
7
7
|
end unless method_defined? :try_convert
|
8
8
|
end
|
9
9
|
|
10
|
+
# Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
10
11
|
def ascii_only?
|
11
12
|
!(self =~ /[^\x00-\x7f]/)
|
12
13
|
end unless method_defined? :ascii_only?
|
13
14
|
|
15
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
14
16
|
alias_method :bytesize, :length unless method_defined? :bytesize
|
15
17
|
|
16
18
|
# Standard in rails. See official documentation[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html]
|
@@ -22,19 +24,18 @@ class String
|
|
22
24
|
end
|
23
25
|
end unless method_defined? :camelize
|
24
26
|
|
25
|
-
# Standard in
|
27
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
26
28
|
def chr
|
27
29
|
chars.first
|
28
|
-
end unless method_defined? :chr
|
30
|
+
end unless method_defined? :chr
|
29
31
|
|
30
|
-
|
31
|
-
# Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
32
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
32
33
|
def clear
|
33
34
|
self[0,length] = ""
|
34
35
|
self
|
35
36
|
end unless method_defined? :clear?
|
36
37
|
|
37
|
-
# Standard in
|
38
|
+
# Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
38
39
|
def codepoints
|
39
40
|
return to_enum(:codepoints) unless block_given?
|
40
41
|
each_char.each do |s|
|
@@ -68,15 +69,11 @@ class String
|
|
68
69
|
|
69
70
|
make_block_optional :each_byte, :each, :each_line, :test_on => "abc"
|
70
71
|
|
71
|
-
#
|
72
|
-
#
|
73
|
-
#
|
74
|
-
# gsub_without_optional_block(*arg, &block)
|
75
|
-
# end
|
76
|
-
# alias_method_chain :gsub, :optional_block
|
77
|
-
# end
|
72
|
+
# gsub: Left alone because of $~, $1, etc... which needs to be "pushed" up one level
|
73
|
+
# It's possible to do so, but it's a method that is used everywhere so i felt
|
74
|
+
# the performance hit was too big compared to the dubious gain.
|
78
75
|
|
79
|
-
# Standard in
|
76
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
80
77
|
unless method_defined? :each_char
|
81
78
|
def each_char(&block)
|
82
79
|
return to_enum(:each_char) unless block_given?
|
@@ -86,31 +83,31 @@ class String
|
|
86
83
|
alias_method :chars, :each_char unless method_defined? :chars
|
87
84
|
end
|
88
85
|
|
86
|
+
# Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
89
87
|
alias_method :each_codepoint, :codepoints unless method_defined? :each_codepoint
|
90
88
|
|
91
|
-
# Standard in
|
89
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
92
90
|
def end_with?(*suffixes)
|
93
91
|
suffixes.each do |suffix|
|
94
|
-
|
92
|
+
next unless suffix.respond_to? :to_str
|
93
|
+
suffix = suffix.to_str
|
95
94
|
return true if self[-suffix.length, suffix.length] == suffix
|
96
95
|
end
|
97
96
|
false
|
98
97
|
end unless method_defined? :end_with?
|
99
98
|
|
100
|
-
# Standard in
|
99
|
+
# Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
101
100
|
def getbyte(i)
|
102
101
|
self[i]
|
103
102
|
end unless method_defined? :getbyte
|
104
103
|
|
105
|
-
# Standard in
|
104
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
106
105
|
unless ("check partition".partition(" ") rescue false)
|
107
|
-
def partition_with_new_meaning(
|
108
|
-
return partition_without_new_meaning(
|
109
|
-
pattern =
|
110
|
-
|
106
|
+
def partition_with_new_meaning(pattern = Undefined, &block)
|
107
|
+
return partition_without_new_meaning(&block) if pattern == Undefined
|
108
|
+
pattern = Type.coerce_to(pattern, String, :to_str) unless pattern.is_a? Regexp
|
111
109
|
i = index(pattern)
|
112
110
|
return [self, "", ""] unless i
|
113
|
-
|
114
111
|
if pattern.is_a? Regexp
|
115
112
|
match = Regexp.last_match
|
116
113
|
[match.pre_match, match[0], match.post_match]
|
@@ -122,8 +119,9 @@ class String
|
|
122
119
|
alias_method_chain :partition, :new_meaning
|
123
120
|
end
|
124
121
|
|
125
|
-
# Standard in
|
122
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
126
123
|
def rpartition(pattern)
|
124
|
+
pattern = Type.coerce_to(pattern, String, :to_str) unless pattern.is_a? Regexp
|
127
125
|
i = rindex(pattern)
|
128
126
|
return ["", "", self] unless i
|
129
127
|
|
@@ -137,10 +135,11 @@ class String
|
|
137
135
|
end unless method_defined? :rpartition
|
138
136
|
|
139
137
|
|
140
|
-
# Standard in
|
138
|
+
# Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/String.html]
|
141
139
|
def start_with?(*prefixes)
|
142
140
|
prefixes.each do |prefix|
|
143
|
-
|
141
|
+
next unless prefix.respond_to? :to_str
|
142
|
+
prefix = prefix.to_str
|
144
143
|
return true if self[0, prefix.length] == prefix
|
145
144
|
end
|
146
145
|
false
|
@@ -157,6 +156,7 @@ class String
|
|
157
156
|
|
158
157
|
unless ("abc".upto("def", true) rescue false)
|
159
158
|
def upto_with_exclusive(to, excl=false, &block)
|
159
|
+
return upto_without_exclusive(to, &block) if block_given? && !excl
|
160
160
|
enum = Range.new(self, to, excl).to_enum
|
161
161
|
return enum unless block_given?
|
162
162
|
enum.each(&block)
|
data/lib/backports/symbol.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
class Symbol
|
2
|
-
# Standard in ruby 1.
|
2
|
+
# Standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Symbol.html]
|
3
3
|
def to_proc
|
4
4
|
Proc.new { |*args| args.shift.__send__(self, *args) }
|
5
5
|
end unless :to_proc.respond_to?(:to_proc)
|
data/lib/backports.rb
CHANGED
@@ -10,13 +10,6 @@ module Kernel
|
|
10
10
|
end unless method_defined? :require_relative
|
11
11
|
end
|
12
12
|
|
13
|
-
|
14
|
-
# Standard in rails, and we use it in module.
|
15
|
-
def extract_options!
|
16
|
-
last.is_a?(::Hash) ? pop : {}
|
17
|
-
end unless method_defined? :extract_options!
|
18
|
-
end
|
19
|
-
|
20
|
-
%w(object module kernel object_space array enumerable enumerator string symbol integer fixnum hash proc binding dir io method regexp struct).each do |lib|
|
13
|
+
%w(core_ext module kernel array enumerable enumerator string symbol integer numeric fixnum hash proc binding dir io method regexp struct float object_space argf gc env process).each do |lib|
|
21
14
|
require_relative "backports/#{lib}"
|
22
15
|
end
|
data/test/enumerable_test.rb
CHANGED
@@ -99,7 +99,7 @@ class EnumerableTest < Test::Unit::TestCase
|
|
99
99
|
assert_equal nil, %w{ant bat cat dog}.find_index {|item| item =~ /h/ }
|
100
100
|
end
|
101
101
|
|
102
|
-
should "
|
102
|
+
should "work for enumerables too" do
|
103
103
|
assert_equal 69-42, (42..666).find_index(69)
|
104
104
|
end
|
105
105
|
end
|
data/test/enumerator_test.rb
CHANGED
@@ -25,5 +25,14 @@ class EnumeratorTest < Test::Unit::TestCase
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
28
|
+
|
29
|
+
context "#next" do
|
30
|
+
should "conform to doc" do
|
31
|
+
enum = [10, 20].to_enum
|
32
|
+
assert_equal 10, enum.next
|
33
|
+
assert_equal 20, enum.next
|
34
|
+
assert_raise(StopIteration){ enum.next}
|
35
|
+
end
|
36
|
+
end
|
28
37
|
end
|
29
38
|
end
|
data/test/kernel_test.rb
CHANGED
@@ -8,14 +8,15 @@ end
|
|
8
8
|
class KernelTest < Test::Unit::TestCase
|
9
9
|
context "Kernel" do
|
10
10
|
context ".loop" do
|
11
|
-
should "
|
12
|
-
|
13
|
-
enum2 = [10, 20].to_enum
|
11
|
+
should "catch StopIteration" do
|
12
|
+
i = 0
|
14
13
|
r = []
|
15
14
|
loop do
|
16
|
-
r <<
|
15
|
+
r << i
|
16
|
+
i += 1
|
17
|
+
raise StopIteration if i > 2
|
17
18
|
end
|
18
|
-
assert_equal [
|
19
|
+
assert_equal [0, 1, 2], r
|
19
20
|
end
|
20
21
|
end
|
21
22
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: marcandre-backports
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.7.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: 2009-05-
|
12
|
+
date: 2009-05-28 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -23,27 +23,36 @@ extra_rdoc_files:
|
|
23
23
|
- LICENSE
|
24
24
|
- README.rdoc
|
25
25
|
files:
|
26
|
+
- .document
|
27
|
+
- .gitignore
|
26
28
|
- CHANGELOG.rdoc
|
27
29
|
- LICENSE
|
28
30
|
- README.rdoc
|
29
31
|
- Rakefile
|
30
32
|
- VERSION.yml
|
33
|
+
- backports.gemspec
|
31
34
|
- lib/backports.rb
|
35
|
+
- lib/backports/argf.rb
|
32
36
|
- lib/backports/array.rb
|
33
37
|
- lib/backports/binding.rb
|
38
|
+
- lib/backports/core_ext.rb
|
34
39
|
- lib/backports/dir.rb
|
35
40
|
- lib/backports/enumerable.rb
|
36
41
|
- lib/backports/enumerator.rb
|
42
|
+
- lib/backports/env.rb
|
37
43
|
- lib/backports/fixnum.rb
|
44
|
+
- lib/backports/float.rb
|
45
|
+
- lib/backports/gc.rb
|
38
46
|
- lib/backports/hash.rb
|
39
47
|
- lib/backports/integer.rb
|
40
48
|
- lib/backports/io.rb
|
41
49
|
- lib/backports/kernel.rb
|
42
50
|
- lib/backports/method.rb
|
43
51
|
- lib/backports/module.rb
|
44
|
-
- lib/backports/
|
52
|
+
- lib/backports/numeric.rb
|
45
53
|
- lib/backports/object_space.rb
|
46
54
|
- lib/backports/proc.rb
|
55
|
+
- lib/backports/process.rb
|
47
56
|
- lib/backports/range.rb
|
48
57
|
- lib/backports/regexp.rb
|
49
58
|
- lib/backports/string.rb
|
data/lib/backports/object.rb
DELETED
@@ -1,37 +0,0 @@
|
|
1
|
-
require 'enumerator'
|
2
|
-
# Must be defined outside of Kernel for jruby, see http://jira.codehaus.org/browse/JRUBY-3609
|
3
|
-
Enumerator = Enumerable::Enumerator unless Kernel.const_defined? :Enumerator # Standard in ruby 1.9
|
4
|
-
|
5
|
-
module Kernel # Did you know that object instance methods are defined in Kernel?
|
6
|
-
|
7
|
-
# Standard in rails. See official documentation[http://api.rubyonrails.org/classes/Object.html]
|
8
|
-
def try(method_id, *args, &block)
|
9
|
-
send(method_id, *args, &block) unless self.nil? #todo: check new def
|
10
|
-
end unless method_defined? :try
|
11
|
-
|
12
|
-
# Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Object.html]
|
13
|
-
def tap
|
14
|
-
yield self
|
15
|
-
self
|
16
|
-
end unless method_defined? :tap
|
17
|
-
|
18
|
-
# Standard in rails. See official documentation[http://api.rubyonrails.org/classes/Object.html]
|
19
|
-
def returning(obj)
|
20
|
-
yield obj
|
21
|
-
obj
|
22
|
-
end unless method_defined? :returning
|
23
|
-
|
24
|
-
# Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Object.html]
|
25
|
-
def define_singleton_method(symbol, &block)
|
26
|
-
class << self
|
27
|
-
self
|
28
|
-
end.send(:define_method, symbol, block)
|
29
|
-
end unless method_defined? :define_singleton_method
|
30
|
-
|
31
|
-
# Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Object.html]
|
32
|
-
def instance_exec(*arg, &block)
|
33
|
-
define_singleton_method(:"temporary method for instance_exec", &block)
|
34
|
-
send(:"temporary method for instance_exec", *arg)
|
35
|
-
end unless method_defined? :instance_exec
|
36
|
-
|
37
|
-
end
|