marcandre-backports 1.0.1 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +14 -0
- data/README.rdoc +9 -3
- data/Rakefile +91 -0
- data/VERSION.yml +1 -1
- data/lib/backports/array.rb +59 -0
- data/lib/backports/enumerable.rb +13 -4
- data/lib/backports/hash.rb +11 -0
- data/lib/backports.rb +1 -1
- data/test/backports_test.rb +32 -0
- metadata +13 -11
data/CHANGELOG.rdoc
ADDED
data/README.rdoc
CHANGED
@@ -10,11 +10,11 @@ Conditions for inclusion:
|
|
10
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
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.
|
12
12
|
|
13
|
-
More complex features of activesupport (even things like String::pluralize), won't be included. <tt>require 'activesupport'</tt> if you need them!
|
13
|
+
More complex features of activesupport (even things like <tt>String::pluralize</tt>), won't be included. <tt>require 'activesupport'</tt> if you need them!
|
14
14
|
|
15
15
|
I've added those as I need them; pull requests welcome (with tests for ruby 1.9 backports)
|
16
16
|
|
17
|
-
==
|
17
|
+
== Compatibility
|
18
18
|
|
19
19
|
Works with ruby 1.8 & 1.9
|
20
20
|
|
@@ -37,15 +37,21 @@ Works with ruby 1.8 & 1.9
|
|
37
37
|
* Hash
|
38
38
|
* <tt>Hash[[[:foo, :bar],[:hello, "world"]]] ==> {:foo => :bar, :hello => "world"}</tt>
|
39
39
|
* +key+
|
40
|
-
* +symbolize_keys+,
|
40
|
+
* +symbolize_keys+, <tt>symbolize_keys!</tt>
|
41
|
+
* +reverse_merge+, <tt>reverse_merge!</tt>
|
41
42
|
* Enumerable
|
42
43
|
* +sum+
|
43
44
|
* +find_index+
|
44
45
|
* +take+, +take_while+, +drop+, +drop_while+
|
45
46
|
* +first+
|
47
|
+
* Array
|
48
|
+
* +flatten+, <tt>flatten!</tt>
|
49
|
+
* +find_index+, +find+
|
46
50
|
* Fixnum
|
47
51
|
* <tt>odd?</tt>, <tt>even?</tt>
|
48
52
|
|
53
|
+
Finally, there is no need to <tt>require 'enumerator'</tt> in older ruby, and +Enumerator+ can be accessed directly (instead of <tt>Enumerable::Enumerator</p>)
|
54
|
+
|
49
55
|
= License
|
50
56
|
|
51
57
|
+backports+ is released under the terms of the MIT License, see the included LICENSE file.
|
data/Rakefile
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'rubygems'
|
3
|
+
require 'rake'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'jeweler'
|
7
|
+
Jeweler::Tasks.new do |gem|
|
8
|
+
gem.name = "backports"
|
9
|
+
gem.summary = "Backports or ruby 1.8.7+ & rails for older ruby."
|
10
|
+
gem.email = "github@marc-andre.ca"
|
11
|
+
gem.homepage = "http://github.com/marcandre/backports"
|
12
|
+
gem.authors = ["Marc-André Lafortune"]
|
13
|
+
gem.rubyforge_project = "backports"
|
14
|
+
|
15
|
+
gem.description = <<-EOS
|
16
|
+
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.
|
17
|
+
EOS
|
18
|
+
gem.has_rdoc = true
|
19
|
+
gem.rdoc_options << '--title' << 'Backports library' <<
|
20
|
+
'--main' << 'README.rdoc' <<
|
21
|
+
'--line-numbers' << '--inline-source'
|
22
|
+
|
23
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
24
|
+
end
|
25
|
+
rescue LoadError
|
26
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
27
|
+
end
|
28
|
+
|
29
|
+
require 'rake/testtask'
|
30
|
+
Rake::TestTask.new(:test) do |test|
|
31
|
+
test.libs << 'lib' << 'test'
|
32
|
+
test.pattern = 'test/**/*_test.rb'
|
33
|
+
test.verbose = false
|
34
|
+
end
|
35
|
+
|
36
|
+
begin
|
37
|
+
require 'rcov/rcovtask'
|
38
|
+
Rcov::RcovTask.new do |test|
|
39
|
+
test.libs << 'test'
|
40
|
+
test.pattern = 'test/**/*_test.rb'
|
41
|
+
test.verbose = true
|
42
|
+
end
|
43
|
+
rescue LoadError
|
44
|
+
task :rcov do
|
45
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
task :default => :test
|
51
|
+
|
52
|
+
require 'rake/rdoctask'
|
53
|
+
Rake::RDocTask.new do |rdoc|
|
54
|
+
if File.exist?('VERSION.yml')
|
55
|
+
config = YAML.load(File.read('VERSION.yml'))
|
56
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
57
|
+
else
|
58
|
+
version = ""
|
59
|
+
end
|
60
|
+
|
61
|
+
rdoc.rdoc_dir = 'rdoc'
|
62
|
+
rdoc.title = "backports #{version}"
|
63
|
+
rdoc.rdoc_files.include('README*')
|
64
|
+
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
|
data/VERSION.yml
CHANGED
@@ -0,0 +1,59 @@
|
|
1
|
+
class Array
|
2
|
+
# flatten & flatten!, standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]
|
3
|
+
unless ([[]].flatten(1) rescue false)
|
4
|
+
|
5
|
+
# Recursively flatten any contained Arrays into an one-dimensional result.
|
6
|
+
# Adapted from rubinius'
|
7
|
+
def flatten_with_optional_argument(level=nil)
|
8
|
+
return flatten_without_optional_argument unless level || level == (1/0.0)
|
9
|
+
dup.flatten!(level) || self
|
10
|
+
end
|
11
|
+
|
12
|
+
# Flattens self in place as #flatten. If no changes are
|
13
|
+
# made, returns nil, otherwise self.
|
14
|
+
# Adapted from rubinius'
|
15
|
+
def flatten_with_optional_argument!(level=nil)
|
16
|
+
return flatten_without_optional_argument! unless level || level == (1/0.0)
|
17
|
+
|
18
|
+
ret, out = nil, []
|
19
|
+
ret = recursively_flatten_finite(self, out, level)
|
20
|
+
replace(out) if ret
|
21
|
+
ret
|
22
|
+
end
|
23
|
+
|
24
|
+
alias_method_chain :flatten, :optional_argument
|
25
|
+
alias_method_chain :flatten!, :optional_argument
|
26
|
+
|
27
|
+
# Helper to recurse through flattening since the method
|
28
|
+
# is not allowed to recurse itself. Detects recursive structures.
|
29
|
+
# Adapted from rubinius'; recursion guards are not needed because level is finite
|
30
|
+
def recursively_flatten_finite(array, out, level)
|
31
|
+
ret = nil
|
32
|
+
if level <= 0
|
33
|
+
out.concat(array)
|
34
|
+
else
|
35
|
+
array.each do |o|
|
36
|
+
if o.respond_to? :to_ary
|
37
|
+
recursively_flatten_finite(o.to_ary, out, level - 1)
|
38
|
+
ret = self
|
39
|
+
else
|
40
|
+
out << o
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
ret
|
45
|
+
end
|
46
|
+
private :recursively_flatten_finite
|
47
|
+
end # flatten & flatten!
|
48
|
+
|
49
|
+
# index
|
50
|
+
unless ([1].index{true} rescue false)
|
51
|
+
def index_with_block(*arg)
|
52
|
+
return index_without_block(*arg) unless block_given? && arg.empty?
|
53
|
+
each_with_index{|o,i| return i if yield o}
|
54
|
+
return nil
|
55
|
+
end
|
56
|
+
alias_method_chain :index, :block
|
57
|
+
alias_method :find_index, :index
|
58
|
+
end
|
59
|
+
end
|
data/lib/backports/enumerable.rb
CHANGED
@@ -11,10 +11,19 @@ module Enumerable
|
|
11
11
|
end unless method_defined? :sum
|
12
12
|
|
13
13
|
# Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html]
|
14
|
-
def find_index(
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
def find_index(*args)
|
15
|
+
if args.size == 1
|
16
|
+
obj = args.first
|
17
|
+
each_with_index do |element, i|
|
18
|
+
return i if element == obj
|
19
|
+
end
|
20
|
+
elsif block_given?
|
21
|
+
each_with_index do |element, i|
|
22
|
+
return i if yield element
|
23
|
+
end
|
24
|
+
each_with_index{|o,i| return i if yield o}
|
25
|
+
else
|
26
|
+
raise ArgumentError, "Wrong number of arguments (#{args.size} for 1)"
|
18
27
|
end
|
19
28
|
nil
|
20
29
|
end unless method_defined? :find_index
|
data/lib/backports/hash.rb
CHANGED
@@ -23,4 +23,15 @@ class Hash
|
|
23
23
|
|
24
24
|
# Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Hash.html]
|
25
25
|
alias_method :key, :index unless method_defined? :key
|
26
|
+
|
27
|
+
# Standard in rails. See official documentation[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Hash/Keys.html]
|
28
|
+
def reverse_merge(other_hash)
|
29
|
+
other_hash.merge(self)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Standard in rails. See official documentation[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Hash/Keys.html]
|
33
|
+
def reverse_merge!(other_hash)
|
34
|
+
replace(reverse_merge(other_hash))
|
35
|
+
end
|
36
|
+
|
26
37
|
end
|
data/lib/backports.rb
CHANGED
@@ -10,6 +10,6 @@ module Kernel
|
|
10
10
|
end unless method_defined? :require_relative
|
11
11
|
end
|
12
12
|
|
13
|
-
%w(object module enumerable string symbol fixnum hash).each do |lib|
|
13
|
+
%w(object module enumerable array string symbol fixnum hash).each do |lib|
|
14
14
|
require_relative "backports/#{lib}"
|
15
15
|
end
|
data/test/backports_test.rb
CHANGED
@@ -6,6 +6,10 @@ class BackportsTest < Test::Unit::TestCase
|
|
6
6
|
assert_equal 3, %w{ant bat cat dog}.find_index {|item| item =~ /g/ }
|
7
7
|
assert_equal nil, %w{ant bat cat dog}.find_index {|item| item =~ /h/ }
|
8
8
|
end
|
9
|
+
|
10
|
+
should "work for enumerables too" do
|
11
|
+
assert_equal 69-42, (42..666).find_index(69)
|
12
|
+
end
|
9
13
|
end
|
10
14
|
|
11
15
|
context "take" do
|
@@ -62,4 +66,32 @@ class BackportsTest < Test::Unit::TestCase
|
|
62
66
|
assert_equal({1 => 2, 3 => 4}, Hash[[[1,2],[3,4]]])
|
63
67
|
end
|
64
68
|
end
|
69
|
+
|
70
|
+
context "flatten" do
|
71
|
+
should "conform to doc" do
|
72
|
+
s = [ 1, 2, 3 ] #=> [1, 2, 3]
|
73
|
+
t = [ 4, 5, 6, [7, 8] ] #=> [4, 5, 6, [7, 8]]
|
74
|
+
a = [ s, t, 9, 10 ] #=> [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]
|
75
|
+
assert_equal [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], a.flatten
|
76
|
+
a = [ 1, 2, [3, [4, 5] ] ]
|
77
|
+
assert_equal [1, 2, 3, [4, 5]], a.flatten(1)
|
78
|
+
end
|
79
|
+
|
80
|
+
should "conform work for recursive arrays" do
|
81
|
+
x=[]
|
82
|
+
x.push(x,x)
|
83
|
+
4.times {|n| assert_equal 2 << n, x.flatten(n).length}
|
84
|
+
assert_raises(ArgumentError) {x.flatten}
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context "index" do
|
89
|
+
should "conform to doc" do
|
90
|
+
a = [ "a", "b", "c" ]
|
91
|
+
assert_equal 1, a.index("b")
|
92
|
+
assert_equal nil, a.index("z")
|
93
|
+
assert_equal 1, a.index{|x|x=="b"}
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
65
97
|
end
|
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.1.1
|
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-04-
|
12
|
+
date: 2009-04-11 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -20,12 +20,16 @@ executables: []
|
|
20
20
|
extensions: []
|
21
21
|
|
22
22
|
extra_rdoc_files:
|
23
|
-
- README.rdoc
|
24
23
|
- LICENSE
|
24
|
+
- README.rdoc
|
25
25
|
files:
|
26
|
+
- CHANGELOG.rdoc
|
27
|
+
- LICENSE
|
26
28
|
- README.rdoc
|
29
|
+
- Rakefile
|
27
30
|
- VERSION.yml
|
28
|
-
- lib/backports
|
31
|
+
- lib/backports.rb
|
32
|
+
- lib/backports/array.rb
|
29
33
|
- lib/backports/enumerable.rb
|
30
34
|
- lib/backports/fixnum.rb
|
31
35
|
- lib/backports/hash.rb
|
@@ -33,22 +37,19 @@ files:
|
|
33
37
|
- lib/backports/object.rb
|
34
38
|
- lib/backports/string.rb
|
35
39
|
- lib/backports/symbol.rb
|
36
|
-
- lib/backports.rb
|
37
40
|
- test/backports_test.rb
|
38
41
|
- test/test_helper.rb
|
39
|
-
- LICENSE
|
40
42
|
has_rdoc: true
|
41
43
|
homepage: http://github.com/marcandre/backports
|
42
44
|
post_install_message:
|
43
45
|
rdoc_options:
|
46
|
+
- --charset=UTF-8
|
44
47
|
- --title
|
45
48
|
- Backports library
|
46
49
|
- --main
|
47
50
|
- README.rdoc
|
48
51
|
- --line-numbers
|
49
52
|
- --inline-source
|
50
|
-
- --inline-source
|
51
|
-
- --charset=UTF-8
|
52
53
|
require_paths:
|
53
54
|
- lib
|
54
55
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -65,10 +66,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
65
66
|
version:
|
66
67
|
requirements: []
|
67
68
|
|
68
|
-
rubyforge_project:
|
69
|
+
rubyforge_project: backports
|
69
70
|
rubygems_version: 1.2.0
|
70
71
|
signing_key:
|
71
72
|
specification_version: 2
|
72
73
|
summary: Backports or ruby 1.8.7+ & rails for older ruby.
|
73
|
-
test_files:
|
74
|
-
|
74
|
+
test_files:
|
75
|
+
- test/backports_test.rb
|
76
|
+
- test/test_helper.rb
|