backports 2.3.0 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. data/.irbrc +1 -0
  2. data/README.rdoc +55 -3
  3. data/Rakefile +1 -0
  4. data/VERSION.yml +1 -1
  5. data/backports.gemspec +99 -118
  6. data/lib/backports/1.8.7/string.rb +1 -1
  7. data/lib/backports/1.9.1/array.rb +1 -2
  8. data/lib/backports/1.9.1/file.rb +20 -0
  9. data/lib/backports/1.9.1/float.rb +19 -0
  10. data/lib/backports/1.9.1/hash.rb +20 -3
  11. data/lib/backports/1.9.1/integer.rb +19 -0
  12. data/lib/backports/1.9.1/io.rb +18 -3
  13. data/lib/backports/1.9.1/numeric.rb +9 -0
  14. data/lib/backports/1.9.1/regexp.rb +1 -6
  15. data/lib/backports/1.9.1/stdlib/prime.rb +495 -0
  16. data/lib/backports/1.9.1/stdlib.rb +1 -0
  17. data/lib/backports/1.9.1/string.rb +2 -7
  18. data/lib/backports/1.9.2/array.rb +3 -4
  19. data/lib/backports/1.9.2/complex.rb +6 -0
  20. data/lib/backports/1.9.2/stdlib/matrix/eigenvalue_decomposition.rb +886 -0
  21. data/lib/backports/1.9.2/stdlib/matrix/lup_decomposition.rb +218 -0
  22. data/lib/backports/1.9.2/stdlib/matrix.rb +1872 -0
  23. data/lib/backports/1.9.2/stdlib/set.rb +13 -0
  24. data/lib/backports/1.9.2/stdlib.rb +1 -0
  25. data/lib/backports/1.9.3/io.rb +12 -0
  26. data/lib/backports/1.9.3.rb +5 -0
  27. data/lib/backports/1.9.rb +1 -1
  28. data/lib/backports/basic_object.rb +3 -2
  29. data/lib/backports/force/array_map.rb +1 -0
  30. data/lib/backports/force/enumerable_map.rb +3 -0
  31. data/lib/backports/force/hash_select.rb +9 -0
  32. data/lib/backports/force/string_length.rb +10 -0
  33. data/lib/backports/force/string_size.rb +1 -0
  34. data/lib/backports/tools.rb +137 -1
  35. data/test/README +13 -0
  36. metadata +25 -42
  37. data/.gitignore +0 -7
  38. data/test/_README +0 -1
  39. data/test/array_test.rb +0 -82
  40. data/test/basic_object_test.rb +0 -70
  41. data/test/binding_test.rb +0 -20
  42. data/test/enumerable_test.rb +0 -244
  43. data/test/enumerator_test.rb +0 -45
  44. data/test/hash_test.rb +0 -26
  45. data/test/kernel_test.rb +0 -31
  46. data/test/math_test.rb +0 -59
  47. data/test/method_missing_test.rb +0 -37
  48. data/test/method_test.rb +0 -73
  49. data/test/module_test.rb +0 -20
  50. data/test/object_test.rb +0 -35
  51. data/test/proc_test.rb +0 -116
  52. data/test/regexp_test.rb +0 -14
  53. data/test/string_test.rb +0 -74
  54. data/test/symbol_test.rb +0 -23
  55. data/test/test_helper.rb +0 -8
data/.irbrc ADDED
@@ -0,0 +1 @@
1
+ $:.unshift "./lib"
data/README.rdoc CHANGED
@@ -23,7 +23,7 @@ Features in this gem:
23
23
 
24
24
  Let's be a bit more precise about the breaking code business. It is of course entirely possible that code will break, for example some core methods are monkeypatched or if the code relies on a certain call raising an exception. Example: <tt>[42].sample rescue "dum example"</tt> will return <tt>"dum example"</tt> without this gem and <tt>42</tt> with it.
25
25
 
26
- A real incompatibility is, for example, <tt>Module::instance_methods</tt> which returns strings in 1.8 and symbols in 1.9. No change can be made without the risk of breaking existing code. Such incompatibilities are left unchanged.
26
+ A real incompatibility is, for example, <tt>Module::instance_methods</tt> which returns strings in 1.8 and symbols in 1.9. No change can be made without the risk of breaking existing code. Such incompatibilities are left unchanged, although you can require some of these changes in addition (see below)
27
27
 
28
28
  All features of 1.8.7 are backported (well, almost all, see the exception list bellow), and many of 1.9 are also.
29
29
 
@@ -75,15 +75,25 @@ Additionally, the following Ruby 1.9 features have been backported:
75
75
  * +binread+
76
76
  * +to_path+
77
77
  * All class methods accepting filenames will accept files or anything with a <tt>#to_path</tt> method.
78
+ * <tt>File.open</tt> accepts an options hash.
79
+
80
+ * Float
81
+ * +round+
78
82
 
79
83
  * Hash
84
+ * +assoc+, +rassoc+
80
85
  * +try_convert+
81
86
  * <tt>default_proc=</tt>
82
87
 
88
+ * Integer
89
+ * +magnitude+
90
+ * +round+
91
+
83
92
  * IO
84
93
  * +bin_read+
85
94
  * +try_convert+
86
95
  * +ungetbyte+
96
+ * <tt>IO.open</tt> accepts an options hash.
87
97
 
88
98
  * Kernel
89
99
  * +require_relative+
@@ -92,6 +102,9 @@ Additionally, the following Ruby 1.9 features have been backported:
92
102
  * +log+ (with base)
93
103
  * +log2+
94
104
 
105
+ * Numeric
106
+ * +round+
107
+
95
108
  * Object
96
109
  * +define_singleton_method+
97
110
  * +public_method+
@@ -139,6 +152,9 @@ Finally, most of Ruby 1.9.2 features have been backported:
139
152
  * <tt>sort_by!</tt>
140
153
  * <tt>uniq, uniq!</tt> (with block)
141
154
 
155
+ * Complex
156
+ * +to_r+
157
+
142
158
  * Dir
143
159
  * +home+
144
160
 
@@ -160,6 +176,13 @@ Finally, most of Ruby 1.9.2 features have been backported:
160
176
 
161
177
  To include all Ruby backports but not those of Rails, <tt>require "backports/1.9.2"</tt> (or "backports/1.9")
162
178
 
179
+ == Ruby 1.9.3
180
+
181
+ Some features of Ruby 1.9.3 have been backported:
182
+
183
+ * IO
184
+ * +write+, +binwrite+
185
+
163
186
  == Rails
164
187
 
165
188
  Some generic methods from Rails methods have been copied:
@@ -187,11 +210,40 @@ To include _only_ these Rails backports but not those of Ruby, <tt>require "back
187
210
 
188
211
  == Libraries
189
212
 
190
- Libraries have not been backported. I am aware of the following backport gems:
213
+ Libraries are slowly being backported. You simply require them as usual, ideally (but not necessarily) after requiring backports.
214
+
215
+ require "backports"
216
+ require "prime"
217
+ 42.prime? # => false, even in Ruby 1.8.x
218
+
219
+ The following libraries are up to date with Ruby 1.9.3:
220
+
221
+ * Matrix
222
+ * Prime
223
+ * Set
224
+
225
+ I am aware of the following backport gem which will hopefully be part of backports soon:
191
226
 
192
227
  * Net::SMTP for Ruby 1.8.6: smtp_tls[http://seattlerb.rubyforge.org/smtp_tls/]
193
228
 
194
- * Let me know of others...
229
+ == Forcing incompatibilities
230
+
231
+ Some backports would create incompatibilities in their current Ruby version but could be useful in some projects. It is possible to request such incompatible changes. Backports currently supports the following:
232
+
233
+ * Hash
234
+ * +select+ (returns a Hash instead of an Array)
235
+
236
+ * Enumerable / Array
237
+ * +map+ (returns an enumerator when called without a block)
238
+
239
+ * String
240
+ * +length+, +size+ (for UTF-8 support)
241
+
242
+ These must be imported in addition to the backports gem, for example:
243
+
244
+ require "backports"
245
+ require "backports/force/hash_select"
246
+ {}.select{} # => {}, even in Ruby 1.8
195
247
 
196
248
  == Thanks
197
249
 
data/Rakefile CHANGED
@@ -49,6 +49,7 @@ task :default => :test
49
49
 
50
50
  require 'rake/rdoctask'
51
51
  Rake::RDocTask.new do |rdoc|
52
+ require 'yaml'
52
53
  if File.exist?('VERSION.yml')
53
54
  config = YAML.load(File.read('VERSION.yml'))
54
55
  version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
data/VERSION.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  ---
2
2
  :build:
3
3
  :major: 2
4
- :minor: 3
4
+ :minor: 4
5
5
  :patch: 0
data/backports.gemspec CHANGED
@@ -1,144 +1,125 @@
1
1
  # Generated by jeweler
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{backports}
8
- s.version = "2.3.0"
8
+ s.version = "2.4.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{2011-06-23}
12
+ s.date = %q{2012-03-24}
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}
16
16
  s.extra_rdoc_files = [
17
17
  "LICENSE",
18
- "README.rdoc"
18
+ "README.rdoc"
19
19
  ]
20
20
  s.files = [
21
21
  ".document",
22
- ".gitignore",
23
- "CHANGELOG.rdoc",
24
- "LICENSE",
25
- "README.rdoc",
26
- "Rakefile",
27
- "VERSION.yml",
28
- "backports.gemspec",
29
- "lib/backports.rb",
30
- "lib/backports/1.8.7.rb",
31
- "lib/backports/1.8.7/argf.rb",
32
- "lib/backports/1.8.7/array.rb",
33
- "lib/backports/1.8.7/binding.rb",
34
- "lib/backports/1.8.7/dir.rb",
35
- "lib/backports/1.8.7/enumerable.rb",
36
- "lib/backports/1.8.7/enumerator.rb",
37
- "lib/backports/1.8.7/env.rb",
38
- "lib/backports/1.8.7/fixnum.rb",
39
- "lib/backports/1.8.7/float.rb",
40
- "lib/backports/1.8.7/gc.rb",
41
- "lib/backports/1.8.7/hash.rb",
42
- "lib/backports/1.8.7/integer.rb",
43
- "lib/backports/1.8.7/io.rb",
44
- "lib/backports/1.8.7/kernel.rb",
45
- "lib/backports/1.8.7/method.rb",
46
- "lib/backports/1.8.7/module.rb",
47
- "lib/backports/1.8.7/numeric.rb",
48
- "lib/backports/1.8.7/object_space.rb",
49
- "lib/backports/1.8.7/proc.rb",
50
- "lib/backports/1.8.7/process.rb",
51
- "lib/backports/1.8.7/range.rb",
52
- "lib/backports/1.8.7/regexp.rb",
53
- "lib/backports/1.8.7/string.rb",
54
- "lib/backports/1.8.7/struct.rb",
55
- "lib/backports/1.8.7/symbol.rb",
56
- "lib/backports/1.8.8.rb",
57
- "lib/backports/1.8.rb",
58
- "lib/backports/1.9.1.rb",
59
- "lib/backports/1.9.1/array.rb",
60
- "lib/backports/1.9.1/dir.rb",
61
- "lib/backports/1.9.1/enumerable.rb",
62
- "lib/backports/1.9.1/enumerator.rb",
63
- "lib/backports/1.9.1/env.rb",
64
- "lib/backports/1.9.1/file.rb",
65
- "lib/backports/1.9.1/hash.rb",
66
- "lib/backports/1.9.1/integer.rb",
67
- "lib/backports/1.9.1/io.rb",
68
- "lib/backports/1.9.1/kernel.rb",
69
- "lib/backports/1.9.1/math.rb",
70
- "lib/backports/1.9.1/proc.rb",
71
- "lib/backports/1.9.1/range.rb",
72
- "lib/backports/1.9.1/regexp.rb",
73
- "lib/backports/1.9.1/string.rb",
74
- "lib/backports/1.9.1/symbol.rb",
75
- "lib/backports/1.9.2.rb",
76
- "lib/backports/1.9.2/array.rb",
77
- "lib/backports/1.9.2/dir.rb",
78
- "lib/backports/1.9.2/enumerable.rb",
79
- "lib/backports/1.9.2/float.rb",
80
- "lib/backports/1.9.2/hash.rb",
81
- "lib/backports/1.9.2/kernel.rb",
82
- "lib/backports/1.9.2/match_data.rb",
83
- "lib/backports/1.9.2/random.rb",
84
- "lib/backports/1.9.2/random/MT19937.rb",
85
- "lib/backports/1.9.2/random/bits_and_bytes.rb",
86
- "lib/backports/1.9.2/random/implementation.rb",
87
- "lib/backports/1.9.2/random/load.rb",
88
- "lib/backports/1.9.rb",
89
- "lib/backports/basic_object.rb",
90
- "lib/backports/rails.rb",
91
- "lib/backports/rails/array.rb",
92
- "lib/backports/rails/enumerable.rb",
93
- "lib/backports/rails/hash.rb",
94
- "lib/backports/rails/kernel.rb",
95
- "lib/backports/rails/module.rb",
96
- "lib/backports/rails/string.rb",
97
- "lib/backports/tools.rb",
98
- "test/_README",
99
- "test/array_test.rb",
100
- "test/basic_object_test.rb",
101
- "test/binding_test.rb",
102
- "test/enumerable_test.rb",
103
- "test/enumerator_test.rb",
104
- "test/hash_test.rb",
105
- "test/kernel_test.rb",
106
- "test/math_test.rb",
107
- "test/method_missing_test.rb",
108
- "test/method_test.rb",
109
- "test/module_test.rb",
110
- "test/object_test.rb",
111
- "test/proc_test.rb",
112
- "test/regexp_test.rb",
113
- "test/string_test.rb",
114
- "test/symbol_test.rb",
115
- "test/test_helper.rb"
22
+ ".irbrc",
23
+ "CHANGELOG.rdoc",
24
+ "LICENSE",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION.yml",
28
+ "backports.gemspec",
29
+ "lib/backports.rb",
30
+ "lib/backports/1.8.7.rb",
31
+ "lib/backports/1.8.7/argf.rb",
32
+ "lib/backports/1.8.7/array.rb",
33
+ "lib/backports/1.8.7/binding.rb",
34
+ "lib/backports/1.8.7/dir.rb",
35
+ "lib/backports/1.8.7/enumerable.rb",
36
+ "lib/backports/1.8.7/enumerator.rb",
37
+ "lib/backports/1.8.7/env.rb",
38
+ "lib/backports/1.8.7/fixnum.rb",
39
+ "lib/backports/1.8.7/float.rb",
40
+ "lib/backports/1.8.7/gc.rb",
41
+ "lib/backports/1.8.7/hash.rb",
42
+ "lib/backports/1.8.7/integer.rb",
43
+ "lib/backports/1.8.7/io.rb",
44
+ "lib/backports/1.8.7/kernel.rb",
45
+ "lib/backports/1.8.7/method.rb",
46
+ "lib/backports/1.8.7/module.rb",
47
+ "lib/backports/1.8.7/numeric.rb",
48
+ "lib/backports/1.8.7/object_space.rb",
49
+ "lib/backports/1.8.7/proc.rb",
50
+ "lib/backports/1.8.7/process.rb",
51
+ "lib/backports/1.8.7/range.rb",
52
+ "lib/backports/1.8.7/regexp.rb",
53
+ "lib/backports/1.8.7/string.rb",
54
+ "lib/backports/1.8.7/struct.rb",
55
+ "lib/backports/1.8.7/symbol.rb",
56
+ "lib/backports/1.8.8.rb",
57
+ "lib/backports/1.8.rb",
58
+ "lib/backports/1.9.1.rb",
59
+ "lib/backports/1.9.1/array.rb",
60
+ "lib/backports/1.9.1/dir.rb",
61
+ "lib/backports/1.9.1/enumerable.rb",
62
+ "lib/backports/1.9.1/enumerator.rb",
63
+ "lib/backports/1.9.1/env.rb",
64
+ "lib/backports/1.9.1/file.rb",
65
+ "lib/backports/1.9.1/float.rb",
66
+ "lib/backports/1.9.1/hash.rb",
67
+ "lib/backports/1.9.1/integer.rb",
68
+ "lib/backports/1.9.1/io.rb",
69
+ "lib/backports/1.9.1/kernel.rb",
70
+ "lib/backports/1.9.1/math.rb",
71
+ "lib/backports/1.9.1/numeric.rb",
72
+ "lib/backports/1.9.1/proc.rb",
73
+ "lib/backports/1.9.1/range.rb",
74
+ "lib/backports/1.9.1/regexp.rb",
75
+ "lib/backports/1.9.1/stdlib.rb",
76
+ "lib/backports/1.9.1/stdlib/prime.rb",
77
+ "lib/backports/1.9.1/string.rb",
78
+ "lib/backports/1.9.1/symbol.rb",
79
+ "lib/backports/1.9.2.rb",
80
+ "lib/backports/1.9.2/array.rb",
81
+ "lib/backports/1.9.2/complex.rb",
82
+ "lib/backports/1.9.2/dir.rb",
83
+ "lib/backports/1.9.2/enumerable.rb",
84
+ "lib/backports/1.9.2/float.rb",
85
+ "lib/backports/1.9.2/hash.rb",
86
+ "lib/backports/1.9.2/kernel.rb",
87
+ "lib/backports/1.9.2/match_data.rb",
88
+ "lib/backports/1.9.2/random.rb",
89
+ "lib/backports/1.9.2/random/MT19937.rb",
90
+ "lib/backports/1.9.2/random/bits_and_bytes.rb",
91
+ "lib/backports/1.9.2/random/implementation.rb",
92
+ "lib/backports/1.9.2/random/load.rb",
93
+ "lib/backports/1.9.2/stdlib.rb",
94
+ "lib/backports/1.9.2/stdlib/matrix.rb",
95
+ "lib/backports/1.9.2/stdlib/matrix/eigenvalue_decomposition.rb",
96
+ "lib/backports/1.9.2/stdlib/matrix/lup_decomposition.rb",
97
+ "lib/backports/1.9.2/stdlib/set.rb",
98
+ "lib/backports/1.9.3.rb",
99
+ "lib/backports/1.9.3/io.rb",
100
+ "lib/backports/1.9.rb",
101
+ "lib/backports/basic_object.rb",
102
+ "lib/backports/force/array_map.rb",
103
+ "lib/backports/force/enumerable_map.rb",
104
+ "lib/backports/force/hash_select.rb",
105
+ "lib/backports/force/string_length.rb",
106
+ "lib/backports/force/string_size.rb",
107
+ "lib/backports/rails.rb",
108
+ "lib/backports/rails/array.rb",
109
+ "lib/backports/rails/enumerable.rb",
110
+ "lib/backports/rails/hash.rb",
111
+ "lib/backports/rails/kernel.rb",
112
+ "lib/backports/rails/module.rb",
113
+ "lib/backports/rails/string.rb",
114
+ "lib/backports/tools.rb",
115
+ "test/README"
116
116
  ]
117
117
  s.homepage = %q{http://github.com/marcandre/backports}
118
- s.rdoc_options = ["--charset=UTF-8", "--title", "Backports library", "--main", "README.rdoc", "--line-numbers", "--inline-source"]
118
+ s.rdoc_options = ["--title", "Backports library", "--main", "README.rdoc", "--line-numbers", "--inline-source"]
119
119
  s.require_paths = ["lib"]
120
120
  s.rubyforge_project = %q{backports}
121
121
  s.rubygems_version = %q{1.3.7}
122
122
  s.summary = %q{Backports of Ruby 1.8.7+ for older ruby.}
123
- s.test_files = [
124
- "test/array_test.rb",
125
- "test/basic_object_test.rb",
126
- "test/binding_test.rb",
127
- "test/enumerable_test.rb",
128
- "test/enumerator_test.rb",
129
- "test/hash_test.rb",
130
- "test/kernel_test.rb",
131
- "test/math_test.rb",
132
- "test/method_missing_test.rb",
133
- "test/method_test.rb",
134
- "test/module_test.rb",
135
- "test/object_test.rb",
136
- "test/proc_test.rb",
137
- "test/regexp_test.rb",
138
- "test/string_test.rb",
139
- "test/symbol_test.rb",
140
- "test/test_helper.rb"
141
- ]
142
123
 
143
124
  if s.respond_to? :specification_version then
144
125
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -17,7 +17,7 @@ class String
17
17
  unless method_defined? :each_char
18
18
  def each_char
19
19
  return to_enum(:each_char) unless block_given?
20
- scan(/./) {|c| yield c}
20
+ scan(/./m) {|c| yield c}
21
21
  end
22
22
 
23
23
  Backports.alias_method self, :chars, :each_char
@@ -2,8 +2,7 @@ class Array
2
2
  # Standard in Ruby 1.8.8. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]
3
3
  class << self
4
4
  def try_convert(obj)
5
- return nil unless obj.respond_to?(:to_ary)
6
- Backports.coerce_to_ary(obj)
5
+ Backports.try_convert(obj, Array, :to_ary)
7
6
  end unless method_defined? :try_convert
8
7
  end
9
8
  end
@@ -19,5 +19,25 @@ class File
19
19
  :split, :stat, :sticky?, :symlink, :symlink?, :truncate, :writable?,
20
20
  :writable_real?, :zero?
21
21
  end
22
+
23
+ begin
24
+ File.open(__FILE__, :mode => 'r').close
25
+ rescue StandardError
26
+ def open_with_options_hash(file, mode = nil, perm = Backports::Undefined, options = Backports::Undefined)
27
+ mode, perm = Backports.combine_mode_perm_and_option(mode, perm, options)
28
+ if block_given?
29
+ open_without_options_hash(file, mode, perm){|f| yield f}
30
+ else
31
+ open_without_options_hash(file, mode, perm)
32
+ end
33
+ end
34
+
35
+ Backports.alias_method_chain self, :open, :options_hash
36
+ end
37
+ end
38
+
39
+ module Constants
40
+ # In Ruby 1.8, it is defined only on Windows
41
+ BINARY = 0 unless const_defined?(:BINARY)
22
42
  end
23
43
  end
@@ -0,0 +1,19 @@
1
+ class Float
2
+ if instance_method(:round).arity.zero?
3
+ def round_with_digits(ndigits=0)
4
+ ndigits = Backports::coerce_to_int(ndigits)
5
+ case
6
+ when ndigits.zero?
7
+ round_without_digits
8
+ when ndigits < 0
9
+ p = 10 ** -ndigits
10
+ p > abs ? 0 : (self / p).round * p
11
+ else
12
+ p = 10 ** ndigits
13
+ prod = self * p
14
+ prod.infinite? ? self : prod.round.fdiv(p)
15
+ end
16
+ end
17
+ Backports.alias_method_chain self, :round, :digits
18
+ end
19
+ end
@@ -2,8 +2,7 @@ class Hash
2
2
  # Standard in Ruby 1.8.8. See official documentation[http://ruby-doc.org/core-1.9/classes/Hash.html]
3
3
  class << self
4
4
  def try_convert(x)
5
- return nil unless x.respond_to? :to_hash
6
- x.to_hash
5
+ Backports.try_convert(x, Hash, :to_hash)
7
6
  end unless method_defined? :try_convert
8
7
  end
9
8
 
@@ -11,4 +10,22 @@ class Hash
11
10
  def default_proc=(proc)
12
11
  replace(Hash.new(&Backports.coerce_to(proc, Proc, :to_proc)).merge!(self))
13
12
  end unless method_defined? :default_proc=
14
- end
13
+
14
+ # Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Hash.html]
15
+ def assoc(key)
16
+ val = fetch(key) do
17
+ return find do |k, v|
18
+ [k, v] if k == key
19
+ end
20
+ end
21
+ [key, val]
22
+ end unless method_defined? :assoc
23
+
24
+
25
+ # Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Hash.html]
26
+ def rassoc(value)
27
+ k = key(value)
28
+ v = fetch(k){return nil}
29
+ [k, fetch(k)] if k || v == value
30
+ end unless method_defined? :rassoc
31
+ end
@@ -1,3 +1,22 @@
1
1
  class Integer
2
2
  Backports.alias_method self, :magnitude, :abs
3
+
4
+ if instance_method(:round).arity.zero?
5
+ def round_with_digits(ndigits=0)
6
+ ndigits = Backports::coerce_to_int(ndigits)
7
+ case
8
+ when ndigits.zero?
9
+ self
10
+ when ndigits > 0
11
+ Float(self)
12
+ else
13
+ pow = 10 ** (-ndigits)
14
+ remain = self % pow
15
+ comp = self < 0 ? :<= : :<
16
+ remain -= pow unless remain.send(comp, pow / 2)
17
+ self - remain
18
+ end
19
+ end
20
+ Backports.alias_method_chain self, :round, :digits
21
+ end
3
22
  end
@@ -8,10 +8,25 @@ class IO
8
8
  end unless method_defined? :binread
9
9
 
10
10
  def try_convert(obj)
11
- return nil unless obj.respond_to?(:to_io)
12
- Backports.coerce_to(obj, IO, :to_io)
11
+ Backports.try_convert(obj, IO, :to_io)
13
12
  end unless method_defined? :try_convert
13
+
14
+ begin
15
+ File.open(__FILE__) { |f| IO.open(f.fileno, :mode => 'r').close }
16
+ rescue StandardError
17
+ def open_with_options_hash(fd, mode = nil, options = Backports::Undefined)
18
+ mode = Backports.combine_mode_and_option(mode, options)
19
+ # Can't backport autoclose, {internal|external|}encoding
20
+ if block_given?
21
+ open_without_options_hash(fd, mode){|f| yield f}
22
+ else
23
+ open_without_options_hash(fd, mode)
24
+ end
25
+ end
26
+
27
+ Backports.alias_method_chain self, :open, :options_hash
28
+ end
14
29
  end
15
30
 
16
31
  Backports.alias_method self, :ungetbyte, :ungetc
17
- end
32
+ end
@@ -0,0 +1,9 @@
1
+ class Numeric
2
+ if instance_method(:round).arity.zero?
3
+ def round_with_digits(ndigits=0)
4
+ ndigits = Backports::coerce_to_int(ndigits)
5
+ ndigits.zero? ? round_without_digits : Float(self).round(ndigits)
6
+ end
7
+ Backports.alias_method_chain self, :round, :digits
8
+ end
9
+ end
@@ -2,12 +2,7 @@ class Regexp
2
2
  # Standard in Ruby 1.8.8. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]
3
3
  class << self
4
4
  def try_convert(obj)
5
- case
6
- when obj.is_a?(Regexp)
7
- obj
8
- when obj.respond_to?(:to_regexp)
9
- Backports.coerce_to(obj, Regexp, :to_regexp)
10
- end
5
+ Backports.try_convert(obj, Regexp, :to_regexp)
11
6
  end unless method_defined? :try_convert
12
7
  end
13
8
  end