backports 1.7.1 → 1.8.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.
Files changed (67) hide show
  1. data/CHANGELOG.rdoc +7 -0
  2. data/README.rdoc +31 -36
  3. data/Rakefile +4 -27
  4. data/VERSION.yml +2 -2
  5. data/backports.gemspec +52 -32
  6. data/lib/backports.rb +4 -15
  7. data/lib/backports/1.8.7.rb +4 -0
  8. data/lib/backports/{argf.rb → 1.8.7/argf.rb} +8 -8
  9. data/lib/backports/{array.rb → 1.8.7/array.rb} +26 -43
  10. data/lib/backports/{binding.rb → 1.8.7/binding.rb} +0 -0
  11. data/lib/backports/1.8.7/dir.rb +7 -0
  12. data/lib/backports/{enumerable.rb → 1.8.7/enumerable.rb} +22 -38
  13. data/lib/backports/1.8.7/enumerator.rb +19 -0
  14. data/lib/backports/1.8.7/env.rb +3 -0
  15. data/lib/backports/1.8.7/fixnum.rb +11 -0
  16. data/lib/backports/1.8.7/float.rb +4 -0
  17. data/lib/backports/{gc.rb → 1.8.7/gc.rb} +0 -0
  18. data/lib/backports/1.8.7/hash.rb +23 -0
  19. data/lib/backports/1.8.7/integer.rb +23 -0
  20. data/lib/backports/{io.rb → 1.8.7/io.rb} +8 -7
  21. data/lib/backports/1.8.7/kernel.rb +39 -0
  22. data/lib/backports/{method.rb → 1.8.7/method.rb} +8 -8
  23. data/lib/backports/1.8.7/module.rb +7 -0
  24. data/lib/backports/1.8.7/numeric.rb +3 -0
  25. data/lib/backports/1.8.7/object_space.rb +5 -0
  26. data/lib/backports/{proc.rb → 1.8.7/proc.rb} +0 -0
  27. data/lib/backports/{process.rb → 1.8.7/process.rb} +0 -0
  28. data/lib/backports/1.8.7/range.rb +4 -0
  29. data/lib/backports/{regexp.rb → 1.8.7/regexp.rb} +2 -1
  30. data/lib/backports/1.8.7/string.rb +90 -0
  31. data/lib/backports/1.8.7/struct.rb +3 -0
  32. data/lib/backports/1.8.7/symbol.rb +6 -0
  33. data/lib/backports/1.9.rb +5 -0
  34. data/lib/backports/1.9/array.rb +12 -0
  35. data/lib/backports/1.9/enumerable.rb +8 -0
  36. data/lib/backports/1.9/enumerator.rb +15 -0
  37. data/lib/backports/1.9/hash.rb +15 -0
  38. data/lib/backports/1.9/integer.rb +3 -0
  39. data/lib/backports/1.9/kernel.rb +11 -0
  40. data/lib/backports/1.9/string.rb +39 -0
  41. data/lib/backports/{symbol.rb → 1.9/symbol.rb} +1 -6
  42. data/lib/backports/rails.rb +4 -0
  43. data/lib/backports/rails/array.rb +6 -0
  44. data/lib/backports/rails/enumerable.rb +12 -0
  45. data/lib/backports/rails/hash.rb +31 -0
  46. data/lib/backports/rails/kernel.rb +12 -0
  47. data/lib/backports/rails/module.rb +6 -0
  48. data/lib/backports/rails/string.rb +42 -0
  49. data/lib/backports/tools.rb +117 -0
  50. data/test/enumerator_test.rb +3 -3
  51. data/test/test_helper.rb +1 -3
  52. metadata +50 -31
  53. data/lib/backports/core_ext.rb +0 -61
  54. data/lib/backports/dir.rb +0 -7
  55. data/lib/backports/enumerator.rb +0 -43
  56. data/lib/backports/env.rb +0 -3
  57. data/lib/backports/fixnum.rb +0 -11
  58. data/lib/backports/float.rb +0 -4
  59. data/lib/backports/hash.rb +0 -64
  60. data/lib/backports/integer.rb +0 -25
  61. data/lib/backports/kernel.rb +0 -55
  62. data/lib/backports/module.rb +0 -30
  63. data/lib/backports/numeric.rb +0 -3
  64. data/lib/backports/object_space.rb +0 -5
  65. data/lib/backports/range.rb +0 -4
  66. data/lib/backports/string.rb +0 -167
  67. data/lib/backports/struct.rb +0 -3
@@ -1,5 +1,12 @@
1
1
  = Packable --- History
2
2
 
3
+ == Version 1.8 - August 30, 2009
4
+
5
+ Backports can be required selectively:
6
+ * backports/1.8.7
7
+ * backports/1.9 (which include 1.8.7)
8
+ * rails
9
+
3
10
  == Version 1.7.1 - August 20, 2009
4
11
 
5
12
  Enumerable#take and Enumerable#first(n) are now returning as soon as possible. Previously, #each was yielding one element too many. See redmine issue #1554.
@@ -1,18 +1,32 @@
1
1
  = Backports Library
2
2
 
3
- Essential backports that make it possible to use some of the really nice features of Ruby 1.8.7, Ruby 1.9 and rails from Ruby 1.8.x
3
+ Would you like to use features of Ruby 1.8.7 without the need of having 1.8.7 installed? Simply
4
4
 
5
- Conditions for inclusion:
6
- 1. Standard in either Ruby or Rails
7
- 2. Won't break older code
8
- 3. Simple and self-contained
5
+ require 'backports/1.8.7'
9
6
 
10
- The first and second rules avoids conflicts in future and the past respectively. Because of the second rule, incompatibilities between 1.8 and 1.9 methods are left alone.
11
- For example, <tt>Module::instance_methods</tt> returns strings in 1.8 and symbols in 1.9; no change can be made without the risk of breaking existing code.
7
+ All 1.8.7 features are now available! Some of the features of 1.9 are also available with
12
8
 
13
- More complex features of active-support (even things like <tt>String::pluralize</tt>), won't be included. <tt>require 'activesupport'</tt> if you need them and are not in rails!
9
+ require 'backports/1.9'
10
+
11
+ Finally, some features of rails' active-support are also available with
12
+
13
+ require 'backports/rails'
14
+
15
+ If you want them all:
16
+
17
+ require 'backports'
14
18
 
15
- I've added these backports as I need them; pull requests welcome (with tests for Ruby 1.9 backports)
19
+ == What's inside
20
+
21
+ All features of 1.8.7 are backported (well, almost all, see the exception list bellow).
22
+
23
+ Conditions for inclusion of ruby 1.9 and activesupport features:
24
+ 1. Won't break older code
25
+ 2. Simple and self-contained
26
+
27
+ For example, <tt>Module::instance_methods</tt> returns strings in 1.8 and symbols in 1.9; no change can be made without the risk of breaking existing code. Such incompatibilities between 1.8 and 1.9 methods are left unchanged.
28
+
29
+ More complex features of active-support (even things like <tt>String::pluralize</tt>), won't be included. <tt>require 'activesupport'</tt> if you need them and are not in rails!
16
30
 
17
31
  == Installation & compatibility
18
32
 
@@ -23,58 +37,39 @@ I've added these backports as I need them; pull requests welcome (with tests for
23
37
  To use:
24
38
 
25
39
  require 'rubygems'
26
- require 'backports'
40
+ require 'backports' # or a subset like "backports/1.8.7"
27
41
  # and off you go!
28
42
 
29
- Compatible with Ruby 1.8 & 1.9.
43
+ Compatible with Ruby 1.8 & 1.9, jruby and rubinius.
30
44
 
31
- = List of backports
45
+ = Complete List of backports
32
46
 
33
47
  == Ruby 1.8.7
34
48
 
35
- Complete Ruby 1.8.7 backporting. Refer to the official list of changes[http://svn.ruby-lang.org/repos/ruby/tags/v1_8_7/NEWS]. All builtin classes are compatible with Ruby 1.8.7:
36
- * Array
37
- * Binding
38
- * Dir
39
- * Enumerable
40
- * Enumerator
41
- * Fixnum
42
- * Hash
43
- * Integer
44
- * IO (& ARGF)
45
- * Method
46
- * Numeric
47
- * Object
48
- * ObjectSpace
49
- * Range
50
- * Regexp
51
- * Symbol
52
- * UnboundMethod
49
+ Complete Ruby 1.8.7 backporting. Refer to the official list of changes[http://svn.ruby-lang.org/repos/ruby/tags/v1_8_7/NEWS].
53
50
 
54
51
  Only exceptions:
55
52
  * String#gsub
56
53
  * GC.stress=
57
54
  * Process.exec
55
+ * Array#choice (use Array#sample instead)
58
56
  * recursive data handling (Array and Hash)
59
57
 
60
- As in Ruby 1.8.7+, there is no need to <tt>require 'enumerator'</tt> in older Ruby, and +Enumerator+ can be accessed directly (instead of <tt>Enumerable::Enumerator</tt>)
61
-
62
58
  == Ruby 1.9
63
59
 
64
60
  Additionally, the following Ruby 1.9 have been backported:
65
61
 
66
62
  * Array
67
- * +sample+
63
+ * +try_convert+
68
64
 
69
65
  * Enumerable
70
66
  * +each_with_object+
71
- * +reverse_each+
72
67
 
73
68
  * Enumerator
74
69
  * +new+ (with block)
75
70
 
76
71
  * Hash
77
- * <tt>Hash[[[:foo, :bar],[:hello, "world"]]] ==> {:foo => :bar, :hello => "world"}</tt> (see _note_)
72
+ * +try_convert+
78
73
  * <tt>default_proc=</tt>
79
74
 
80
75
  * Kernel
@@ -93,7 +88,7 @@ Additionally, the following Ruby 1.9 have been backported:
93
88
  * +clear+
94
89
  * +codepoints+, +each_codepoint+
95
90
 
96
- _note_: This usage of <tt>Hash::[]</tt> is not yet documented[http://redmine.ruby-lang.org/issues/show/1385].
91
+ +Enumerator+ can be accessed directly (instead of <tt>Enumerable::Enumerator</tt>)
97
92
 
98
93
  == Rails
99
94
 
data/Rakefile CHANGED
@@ -22,6 +22,9 @@ begin
22
22
 
23
23
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
24
24
  end
25
+ Jeweler::RubyforgeTasks.new do |rubyforge|
26
+ rubyforge.doc_task = "rdoc"
27
+ end
25
28
  rescue LoadError
26
29
  puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
27
30
  end
@@ -62,30 +65,4 @@ Rake::RDocTask.new do |rdoc|
62
65
  rdoc.title = "backports #{version}"
63
66
  rdoc.rdoc_files.include('README*')
64
67
  rdoc.rdoc_files.include('lib/**/*.rb')
65
- end
66
-
67
- begin
68
- require 'rake/contrib/sshpublisher'
69
- namespace :rubyforge do
70
-
71
- desc "Release gem and RDoc documentation to RubyForge"
72
- task :release => ["rubyforge:release:gem", "rubyforge:release:docs"]
73
-
74
- namespace :release do
75
- desc "Publish RDoc to RubyForge."
76
- task :docs => [:rdoc] do
77
- config = YAML.load(
78
- File.read(File.expand_path('~/.rubyforge/user-config.yml'))
79
- )
80
-
81
- host = "#{config['username']}@rubyforge.org"
82
- remote_dir = "/var/www/gforge-projects/backports/"
83
- local_dir = 'rdoc'
84
-
85
- Rake::SshDirPublisher.new(host, remote_dir, local_dir).upload
86
- end
87
- end
88
- end
89
- rescue LoadError
90
- puts "Rake SshDirPublisher is unavailable or your rubyforge environment is not configured."
91
- end
68
+ end
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 1
3
- :minor: 7
4
- :patch: 1
3
+ :minor: 8
4
+ :patch: 0
@@ -1,13 +1,17 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
1
4
  # -*- encoding: utf-8 -*-
2
5
 
3
6
  Gem::Specification.new do |s|
4
7
  s.name = %q{backports}
5
- s.version = "1.7.1"
8
+ s.version = "1.8.0"
6
9
 
7
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
11
  s.authors = ["Marc-Andr\303\251 Lafortune"]
9
- s.date = %q{2009-08-20}
10
- 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.}
12
+ s.date = %q{2009-08-31}
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
+ }
11
15
  s.email = %q{github@marc-andre.ca}
12
16
  s.extra_rdoc_files = [
13
17
  "LICENSE",
@@ -23,32 +27,49 @@ Gem::Specification.new do |s|
23
27
  "VERSION.yml",
24
28
  "backports.gemspec",
25
29
  "lib/backports.rb",
26
- "lib/backports/argf.rb",
27
- "lib/backports/array.rb",
28
- "lib/backports/binding.rb",
29
- "lib/backports/core_ext.rb",
30
- "lib/backports/dir.rb",
31
- "lib/backports/enumerable.rb",
32
- "lib/backports/enumerator.rb",
33
- "lib/backports/env.rb",
34
- "lib/backports/fixnum.rb",
35
- "lib/backports/float.rb",
36
- "lib/backports/gc.rb",
37
- "lib/backports/hash.rb",
38
- "lib/backports/integer.rb",
39
- "lib/backports/io.rb",
40
- "lib/backports/kernel.rb",
41
- "lib/backports/method.rb",
42
- "lib/backports/module.rb",
43
- "lib/backports/numeric.rb",
44
- "lib/backports/object_space.rb",
45
- "lib/backports/proc.rb",
46
- "lib/backports/process.rb",
47
- "lib/backports/range.rb",
48
- "lib/backports/regexp.rb",
49
- "lib/backports/string.rb",
50
- "lib/backports/struct.rb",
51
- "lib/backports/symbol.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.9.rb",
57
+ "lib/backports/1.9/array.rb",
58
+ "lib/backports/1.9/enumerable.rb",
59
+ "lib/backports/1.9/enumerator.rb",
60
+ "lib/backports/1.9/hash.rb",
61
+ "lib/backports/1.9/integer.rb",
62
+ "lib/backports/1.9/kernel.rb",
63
+ "lib/backports/1.9/string.rb",
64
+ "lib/backports/1.9/symbol.rb",
65
+ "lib/backports/rails.rb",
66
+ "lib/backports/rails/array.rb",
67
+ "lib/backports/rails/enumerable.rb",
68
+ "lib/backports/rails/hash.rb",
69
+ "lib/backports/rails/kernel.rb",
70
+ "lib/backports/rails/module.rb",
71
+ "lib/backports/rails/string.rb",
72
+ "lib/backports/tools.rb",
52
73
  "test/array_test.rb",
53
74
  "test/binding_test.rb",
54
75
  "test/enumerable_test.rb",
@@ -63,12 +84,11 @@ Gem::Specification.new do |s|
63
84
  "test/symbol_test.rb",
64
85
  "test/test_helper.rb"
65
86
  ]
66
- s.has_rdoc = true
67
87
  s.homepage = %q{http://github.com/marcandre/backports}
68
88
  s.rdoc_options = ["--charset=UTF-8", "--title", "Backports library", "--main", "README.rdoc", "--line-numbers", "--inline-source"]
69
89
  s.require_paths = ["lib"]
70
90
  s.rubyforge_project = %q{backports}
71
- s.rubygems_version = %q{1.3.1}
91
+ s.rubygems_version = %q{1.3.5}
72
92
  s.summary = %q{Backports or ruby 1.8.7+ & rails for older ruby.}
73
93
  s.test_files = [
74
94
  "test/array_test.rb",
@@ -88,7 +108,7 @@ Gem::Specification.new do |s|
88
108
 
89
109
  if s.respond_to? :specification_version then
90
110
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
91
- s.specification_version = 2
111
+ s.specification_version = 3
92
112
 
93
113
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
94
114
  else
@@ -1,15 +1,4 @@
1
- module Kernel
2
- # Standard in ruby 1.9.
3
- def require_relative(relative_feature)
4
- # Adapted from Pragmatic's "Programming Ruby" (since their version was buggy...)
5
- file = caller.first.split(/:\d/,2).first
6
- if /\A\((.*)\)/ =~ file # eval, etc.
7
- raise LoadError, "require_relative is called in #{$1}"
8
- end
9
- require File.expand_path(relative_feature, File.dirname(file))
10
- end unless method_defined? :require_relative
11
- end
12
-
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|
14
- require_relative "backports/#{lib}"
15
- end
1
+ require File.expand_path(File.dirname(__FILE__) + "/backports/tools")
2
+ Backports.require_relative "backports/1.8.7"
3
+ Backports.require_relative "backports/1.9"
4
+ Backports.require_relative "backports/rails"
@@ -0,0 +1,4 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/tools")
2
+ %w(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|
3
+ Backports.require_relative "1.8.7/#{lib}"
4
+ end
@@ -1,22 +1,22 @@
1
1
  if RUBY_VERSION < '1.8.7'
2
2
  class << ARGF
3
- # Standard in Ruby 1.8.7.
4
- make_block_optional :each, :each_line, :each_byte
3
+ # No official documentation...
4
+ Backports.make_block_optional self, :each, :each_line, :each_byte
5
5
  end
6
6
  end
7
7
 
8
8
  class << ARGF
9
- # Standard in Ruby 1.8.7.
9
+ # No official documentation...
10
10
  def bytes
11
11
  to_enum :each_byte
12
12
  end unless method_defined? :bytes
13
13
 
14
- # Standard in Ruby 1.8.7.
14
+ # No official documentation...
15
15
  def chars
16
16
  to_enum :each_char
17
17
  end unless method_defined? :chars
18
18
 
19
- # Standard in Ruby 1.8.7.
19
+ # No official documentation...
20
20
  def each_char
21
21
  return to_enum(:each_char) unless block_given?
22
22
  if $KCODE == "UTF-8"
@@ -44,13 +44,13 @@ class << ARGF
44
44
  self
45
45
  end unless method_defined? :each_char
46
46
 
47
- # Standard in Ruby 1.8.7.
47
+ # No official documentation...
48
48
  alias_method :getbyte, :getc
49
49
 
50
- # Standard in Ruby 1.8.7.
50
+ # No official documentation...
51
51
  alias_method :readbyte, :readchar
52
52
 
53
- # Standard in Ruby 1.8.7.
53
+ # No official documentation...
54
54
  def lines(*args)
55
55
  to_enum :each_line, *args
56
56
  end unless method_defined? :lines
@@ -1,19 +1,7 @@
1
1
  class Array
2
- # Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]
3
- class << self
4
- # Try to convert obj into an array, using to_ary method.
5
- # Returns converted array or nil if obj cannot be converted
6
- # for any reason. This method is to check if an argument is an array.
7
- def try_convert(obj)
8
- return nil unless obj.respond_to?(:to_ary)
9
- Type.coerce_to(obj, Array, :to_ary)
10
- end
11
- end
12
-
13
-
14
2
  # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]
15
3
  def combination(num)
16
- num = Type.coerce_to num, Fixnum, :to_int
4
+ num = Backports.coerce_to num, Fixnum, :to_int
17
5
  return to_enum(:combination, num) unless block_given?
18
6
  return self unless (0..size).include? num
19
7
  # Implementation note: slightly tricky.
@@ -36,19 +24,14 @@ class Array
36
24
  if n.nil?
37
25
  loop(&block)
38
26
  else
39
- n = Type.coerce_to n, Fixnum, :to_int
27
+ n = Backports.coerce_to n, Fixnum, :to_int
40
28
  n.times{each(&block)}
41
29
  end
42
30
  nil
43
31
  end unless method_defined? :cycle
44
32
 
45
- # Standard in rails, and we use it here...
46
- def extract_options!
47
- last.is_a?(::Hash) ? pop : {}
48
- end unless method_defined? :extract_options!
49
-
50
33
  # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]
51
- make_block_optional :collect!, :map!, :each, :each_index, :reverse_each, :reject, :reject!, :delete_if, :test_on => [42]
34
+ Backports.make_block_optional self, :collect!, :map!, :each, :each_index, :reverse_each, :reject, :reject!, :delete_if, :test_on => [42]
52
35
 
53
36
  # flatten & flatten!, standard in ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]
54
37
  unless ([[]].flatten(1) rescue false)
@@ -63,7 +46,7 @@ class Array
63
46
  # made, returns nil, otherwise self.
64
47
  # Adapted from rubinius'
65
48
  def flatten_with_optional_argument!(level=-1)
66
- level = Type.coerce_to(level, Integer, :to_int)
49
+ level = Backports.coerce_to(level, Integer, :to_int)
67
50
  return flatten_without_optional_argument! unless level >= 0
68
51
 
69
52
  ret, out = nil, []
@@ -72,8 +55,8 @@ class Array
72
55
  ret
73
56
  end
74
57
 
75
- alias_method_chain :flatten, :optional_argument
76
- alias_method_chain :flatten!, :optional_argument
58
+ Backports.alias_method_chain self, :flatten, :optional_argument
59
+ Backports.alias_method_chain self, :flatten!, :optional_argument
77
60
 
78
61
  # Helper to recurse through flattening
79
62
  # Adapted from rubinius'; recursion guards are not needed because level is finite
@@ -103,57 +86,57 @@ class Array
103
86
  each_with_index{|o,i| return i if yield o}
104
87
  return nil
105
88
  end
106
- alias_method_chain :index, :block
89
+ Backports.alias_method_chain self, :index, :block
107
90
  alias_method :find_index, :index
108
91
  end
109
92
 
110
93
  # pop. Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]
111
94
  unless ([1].pop(1) rescue false)
112
- def pop_with_optional_argument(n = Undefined)
113
- return pop_without_optional_argument if n == Undefined
114
- n = Type.coerce_to(n, Fixnum, :to_int)
95
+ def pop_with_optional_argument(n = Backports::Undefined)
96
+ return pop_without_optional_argument if n == Backports::Undefined
97
+ n = Backports.coerce_to(n, Fixnum, :to_int)
115
98
  raise ArgumentError, "negative array size" if n < 0
116
99
  first = size - n
117
100
  first = 0 if first < 0
118
101
  slice!(first..size).to_a
119
102
  end
120
- alias_method_chain :pop, :optional_argument
103
+ Backports.alias_method_chain self, :pop, :optional_argument
121
104
  end
122
105
 
123
- # Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]
106
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]
124
107
  def product(*arg)
125
108
  # Implementation notes: We build an enumerator for all the combinations
126
109
  # by building it up successively using "inject" and starting from a trivial enumerator.
127
110
  # It would be easy to have "product" yield to a block but the standard
128
111
  # simply returns an array, so you'll find a simple call to "to_a" at the end.
129
112
  #
130
- trivial_enum = Enumerator.new{|yielder| yielder.yield [] }
131
- [self, *arg].map{|x| Type.coerce_to(x, Array, :to_ary)}.
113
+ trivial_enum = Enumerator.new(Backports::Yielder.new{|yielder| yielder.yield [] }) # Enumerator.new{...} is 1.9+ only
114
+ [self, *arg].map{|x| Backports.coerce_to(x, Array, :to_ary)}.
132
115
  inject(trivial_enum) do |enum, array|
133
- Enumerator.new do |yielder|
116
+ Enumerator.new(Backports::Yielder.new do |yielder| # Enumerator.new{...} is 1.9+ only
134
117
  enum.each do |partial_product|
135
118
  array.each do |obj|
136
119
  yielder.yield partial_product + [obj]
137
120
  end
138
121
  end
139
- end
122
+ end)
140
123
  end.to_a
141
124
  end unless method_defined? :product
142
125
 
143
- # rindex
126
+ # rindex. Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]
144
127
  unless ([1].rindex{true} rescue false)
145
128
  def rindex_with_block(*arg)
146
129
  return rindex_without_block(*arg) unless block_given? && arg.empty?
147
130
  reverse_each.each_with_index{|o,i| return size - 1 - i if yield o}
148
131
  return nil
149
132
  end
150
- alias_method_chain :rindex, :block
133
+ Backports.alias_method_chain self, :rindex, :block
151
134
  end
152
135
 
153
136
  # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]
154
- def sample(n = Undefined)
155
- return self[rand(size)] if n == Undefined
156
- n = Type.coerce_to(n, Fixnum, :to_int)
137
+ def sample(n = Backports::Undefined)
138
+ return self[rand(size)] if n == Backports::Undefined
139
+ n = Backports.coerce_to(n, Fixnum, :to_int)
157
140
  raise ArgumentError, "negative array size" if n < 0
158
141
  n = size if n > size
159
142
  result = Array.new(self)
@@ -164,16 +147,16 @@ class Array
164
147
  result[n..size] = []
165
148
  result
166
149
  end unless method_defined? :sample
167
-
150
+
168
151
  # shift. Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]
169
152
  unless ([1].shift(1) rescue false)
170
- def shift_with_optional_argument(n = Undefined)
171
- return shift_without_optional_argument if n == Undefined
172
- n = Type.coerce_to(n, Fixnum, :to_int)
153
+ def shift_with_optional_argument(n = Backports::Undefined)
154
+ return shift_without_optional_argument if n == Backports::Undefined
155
+ n = Backports.coerce_to(n, Fixnum, :to_int)
173
156
  raise ArgumentError, "negative array size" if n < 0
174
157
  slice!(0, n)
175
158
  end
176
- alias_method_chain :shift, :optional_argument
159
+ Backports.alias_method_chain self, :shift, :optional_argument
177
160
  end
178
161
 
179
162
  # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]