marcandre-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/{argf.rb → 1.8.7/argf.rb} +8 -8
- data/lib/backports/{array.rb → 1.8.7/array.rb} +26 -43
- 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/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/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.8.7.rb +4 -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/1.9.rb +5 -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/rails.rb +4 -0
- data/lib/backports/tools.rb +117 -0
- data/lib/backports.rb +4 -15
- data/test/enumerator_test.rb +3 -3
- data/test/test_helper.rb +1 -3
- metadata +48 -32
- 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
- /data/lib/backports/{binding.rb → 1.8.7/binding.rb} +0 -0
- /data/lib/backports/{gc.rb → 1.8.7/gc.rb} +0 -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/CHANGELOG.rdoc
CHANGED
@@ -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.
|
data/README.rdoc
CHANGED
@@ -1,18 +1,32 @@
|
|
1
1
|
= Backports Library
|
2
2
|
|
3
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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].
|
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
|
-
* +
|
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
|
-
*
|
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
|
-
|
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
|
data/VERSION.yml
CHANGED
data/backports.gemspec
CHANGED
@@ -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.
|
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-
|
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/
|
27
|
-
"lib/backports/
|
28
|
-
"lib/backports/
|
29
|
-
"lib/backports/
|
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.
|
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 =
|
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,22 +1,22 @@
|
|
1
1
|
if RUBY_VERSION < '1.8.7'
|
2
2
|
class << ARGF
|
3
|
-
#
|
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
|
-
#
|
9
|
+
# No official documentation...
|
10
10
|
def bytes
|
11
11
|
to_enum :each_byte
|
12
12
|
end unless method_defined? :bytes
|
13
13
|
|
14
|
-
#
|
14
|
+
# No official documentation...
|
15
15
|
def chars
|
16
16
|
to_enum :each_char
|
17
17
|
end unless method_defined? :chars
|
18
18
|
|
19
|
-
#
|
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
|
-
#
|
47
|
+
# No official documentation...
|
48
48
|
alias_method :getbyte, :getc
|
49
49
|
|
50
|
-
#
|
50
|
+
# No official documentation...
|
51
51
|
alias_method :readbyte, :readchar
|
52
52
|
|
53
|
-
#
|
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 =
|
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 =
|
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 =
|
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 =
|
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.
|
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|
|
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 =
|
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 =
|
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]
|
@@ -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
|