backports 2.0.3 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -90,6 +90,8 @@ Additionally, the following Ruby 1.9 features have been backported:
90
90
 
91
91
  * Proc
92
92
  * +yield+
93
+ * +lambda?+
94
+ * +curry+
93
95
 
94
96
  * String
95
97
  * <tt>ascii_only?</tt>
@@ -177,6 +179,7 @@ Libraries have not been backported. I am aware of the following backport gems:
177
179
  Thanks for the bug reports and patches, in particular the repeat offenders:
178
180
 
179
181
  * Arto Bendiken ( bendiken[http://github.com/bendiken] )
182
+ * Konstantin Haase ( rkh[https://github.com/rkh])
180
183
  * Roger Pack ( rdp[http://github.com/rdp] )
181
184
 
182
185
  The best way to submit a patch is to also submit a patch to RubySpec[https://github.com/rubyspec/rubyspec] and then a patch to backports that make it pass the spec. To test rubyspec along with backports, one way to test this is:
@@ -1,5 +1,5 @@
1
1
  ---
2
+ :patch: 0
2
3
  :build:
3
4
  :major: 2
4
- :minor: 0
5
- :patch: 3
5
+ :minor: 1
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{backports}
8
- s.version = "2.0.3"
8
+ s.version = "2.1.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-03-28}
12
+ s.date = %q{2011-04-16}
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}
@@ -72,6 +72,7 @@ Gem::Specification.new do |s|
72
72
  "lib/backports/1.9.1/io.rb",
73
73
  "lib/backports/1.9.1/kernel.rb",
74
74
  "lib/backports/1.9.1/math.rb",
75
+ "lib/backports/1.9.1/proc.rb",
75
76
  "lib/backports/1.9.1/string.rb",
76
77
  "lib/backports/1.9.1/symbol.rb",
77
78
  "lib/backports/1.9.2.rb",
@@ -110,6 +111,7 @@ Gem::Specification.new do |s|
110
111
  "test/method_test.rb",
111
112
  "test/module_test.rb",
112
113
  "test/object_test.rb",
114
+ "test/proc_test.rb",
113
115
  "test/regexp_test.rb",
114
116
  "test/string_test.rb",
115
117
  "test/symbol_test.rb",
@@ -119,7 +121,7 @@ Gem::Specification.new do |s|
119
121
  s.rdoc_options = ["--charset=UTF-8", "--title", "Backports library", "--main", "README.rdoc", "--line-numbers", "--inline-source"]
120
122
  s.require_paths = ["lib"]
121
123
  s.rubyforge_project = %q{backports}
122
- s.rubygems_version = %q{1.3.7}
124
+ s.rubygems_version = %q{1.6.0}
123
125
  s.summary = %q{Backports of Ruby 1.8.7+ for older ruby.}
124
126
  s.test_files = [
125
127
  "test/array_test.rb",
@@ -134,6 +136,7 @@ Gem::Specification.new do |s|
134
136
  "test/method_test.rb",
135
137
  "test/module_test.rb",
136
138
  "test/object_test.rb",
139
+ "test/proc_test.rb",
137
140
  "test/regexp_test.rb",
138
141
  "test/string_test.rb",
139
142
  "test/symbol_test.rb",
@@ -141,7 +144,6 @@ Gem::Specification.new do |s|
141
144
  ]
142
145
 
143
146
  if s.respond_to? :specification_version then
144
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
145
147
  s.specification_version = 3
146
148
 
147
149
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
@@ -0,0 +1,52 @@
1
+ unless Proc.method_defined? :lambda?
2
+ class Proc
3
+ # Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Proc.html]
4
+ def lambda?
5
+ !!@is_lambda
6
+ end
7
+ end
8
+
9
+ class Method
10
+ def to_proc_with_lambda_tracking
11
+ proc = to_proc_without_lambda_tracking
12
+ proc.instance_variable_set :@is_lambda, true
13
+ proc
14
+ end
15
+ Backports.alias_method_chain self, :to_proc, :lambda_tracking
16
+ end
17
+
18
+ module Kernel
19
+ def lambda_with_lambda_tracking(&block)
20
+ Backports.track_lambda block, lambda_without_lambda_tracking(&block), true
21
+ end
22
+
23
+ def proc_with_lambda_tracking(&block)
24
+ Backports.track_lambda block, proc_without_lambda_tracking(&block)
25
+ end
26
+
27
+ Backports.alias_method_chain self, :lambda, :lambda_tracking
28
+ Backports.alias_method_chain self, :proc, :lambda_tracking
29
+ end
30
+ end
31
+
32
+ class Proc
33
+ # Standard in Ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Proc.html]
34
+ def curry(argc = nil)
35
+ min_argc = arity < 0 ? -arity - 1 : arity
36
+ argc ||= min_argc
37
+ if lambda? and arity < 0 ? argc < min_argc : argc != arity
38
+ raise ArgumentError, "wrong number of arguments (#{argc} for #{min_argc})"
39
+ end
40
+ creator = lambda? ? :lambda : :proc
41
+ block = send(creator) do |*args|
42
+ if args.count >= argc
43
+ call(*args)
44
+ else
45
+ send(creator) do |*more_args|
46
+ args += more_args
47
+ block.call(*args)
48
+ end
49
+ end
50
+ end
51
+ end unless method_defined? :curry
52
+ end
@@ -175,6 +175,14 @@ module Backports
175
175
  end
176
176
  end
177
177
 
178
+ # Used internally to propagate #lambda?
179
+ def self.track_lambda(from, to, default = false)
180
+ is_lambda = from.instance_variable_get :@is_lambda
181
+ is_lambda = default if is_lambda.nil?
182
+ to.instance_variable_set :@is_lambda, is_lambda
183
+ to
184
+ end
185
+
178
186
  # A simple class which allows the construction of Enumerator from a block
179
187
  class Yielder
180
188
  def initialize(&block)
@@ -0,0 +1,116 @@
1
+ require 'test_helper'
2
+
3
+ class ProcTest < Test::Unit::TestCase
4
+ # method names correspond with Ruby docs
5
+ def n(&b) b.lambda? end
6
+ def m() end
7
+
8
+ class C
9
+ define_method(:d) {}
10
+ define_method(:e, &proc {})
11
+ end
12
+
13
+ def implicit
14
+ Proc.new
15
+ end
16
+
17
+ context "Proc" do
18
+ context "#lambda?" do
19
+ context "basic usage" do
20
+ should "conform to docs" do
21
+ assert lambda {}.lambda?
22
+ assert !proc {}.lambda?
23
+ assert !Proc.new {}.lambda?
24
+ end
25
+ end
26
+
27
+ context "propagation" do
28
+ should "conform to docs" do
29
+ assert lambda(&lambda {}).lambda?
30
+ assert proc(&lambda {}).lambda?
31
+ assert Proc.new(&lambda {}).lambda?
32
+ end
33
+ end
34
+
35
+ context "blocks passed to methods" do
36
+ should "conform to docs" do
37
+ assert !n { }
38
+ assert n(&lambda {})
39
+ assert !n(&proc {})
40
+ assert !n(&Proc.new {})
41
+ end
42
+
43
+ should "not break Proc.new" do
44
+ implicit { }
45
+ implicit(&proc {})
46
+ end
47
+ end
48
+
49
+ context "Method#to_proc" do
50
+ should "conform to docs" do
51
+ assert method(:m).to_proc.lambda?
52
+ assert n(&method(:m))
53
+ assert n(&method(:m).to_proc)
54
+ assert C.new.method(:d).to_proc.lambda?
55
+ assert C.new.method(:e).to_proc.lambda?
56
+ end
57
+ end
58
+ end
59
+
60
+ context "#curry" do
61
+ context "proc" do
62
+ context "no arguments" do
63
+ should "conform to docs" do
64
+ b = proc { :foo }
65
+ assert_equal :foo, b.curry[]
66
+ end
67
+ end
68
+
69
+ context "arity > 0" do
70
+ should "conform to docs" do
71
+ b = Proc.new {|x, y, z| (x||0) + (y||0) + (z||0) }
72
+ assert_equal 6, b.curry[1][2][3]
73
+ assert_equal 6, b.curry[1, 2][3, 4]
74
+ assert_equal 6, b.curry(5)[1][2][3][4][5]
75
+ assert_equal 6, b.curry(5)[1, 2][3, 4][5]
76
+ assert_equal 1, b.curry(1)[1]
77
+ end
78
+ end
79
+
80
+ context "arity < 0" do
81
+ should "conform to docs" do
82
+ b = Proc.new {|x, y, z, *w| (x||0) + (y||0) + (z||0) + w.inject(0, &:+) }
83
+ assert_equal 6, b.curry[1][2][3]
84
+ assert_equal 10, b.curry[1, 2][3, 4]
85
+ assert_equal 15, b.curry(5)[1][2][3][4][5]
86
+ assert_equal 15, b.curry(5)[1, 2][3, 4][5]
87
+ assert_equal 1, b.curry(1)[1]
88
+ end
89
+ end
90
+ end
91
+
92
+ context "lambda" do
93
+ context "arity > 0" do
94
+ should "conform to docs" do
95
+ b = lambda {|x, y, z| (x||0) + (y||0) + (z||0) }
96
+ assert_equal 6, b.curry[1][2][3]
97
+ assert_raises(ArgumentError) { b.curry[1, 2][3, 4] }
98
+ assert_raises(ArgumentError) { b.curry(5) }
99
+ assert_raises(ArgumentError) { b.curry(1) }
100
+ end
101
+ end
102
+
103
+ context "arity < 0" do
104
+ should "conform to docs" do
105
+ b = lambda {|x, y, z, *w| (x||0) + (y||0) + (z||0) + w.inject(0, &:+) }
106
+ assert_equal 6, b.curry[1][2][3]
107
+ assert_equal 10, b.curry[1, 2][3, 4]
108
+ assert_equal 15, b.curry(5)[1][2][3][4][5]
109
+ assert_equal 15, b.curry(5)[1, 2][3, 4][5]
110
+ assert_raises(ArgumentError) { b.curry(1) }
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: backports
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
5
- prerelease: false
4
+ hash: 11
5
+ prerelease:
6
6
  segments:
7
7
  - 2
8
+ - 1
8
9
  - 0
9
- - 3
10
- version: 2.0.3
10
+ version: 2.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Marc-Andr\xC3\xA9 Lafortune"
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-28 00:00:00 -04:00
18
+ date: 2011-04-16 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -83,6 +83,7 @@ files:
83
83
  - lib/backports/1.9.1/io.rb
84
84
  - lib/backports/1.9.1/kernel.rb
85
85
  - lib/backports/1.9.1/math.rb
86
+ - lib/backports/1.9.1/proc.rb
86
87
  - lib/backports/1.9.1/string.rb
87
88
  - lib/backports/1.9.1/symbol.rb
88
89
  - lib/backports/1.9.2.rb
@@ -121,6 +122,7 @@ files:
121
122
  - test/method_test.rb
122
123
  - test/module_test.rb
123
124
  - test/object_test.rb
125
+ - test/proc_test.rb
124
126
  - test/regexp_test.rb
125
127
  - test/string_test.rb
126
128
  - test/symbol_test.rb
@@ -161,7 +163,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
161
163
  requirements: []
162
164
 
163
165
  rubyforge_project: backports
164
- rubygems_version: 1.3.7
166
+ rubygems_version: 1.6.0
165
167
  signing_key:
166
168
  specification_version: 3
167
169
  summary: Backports of Ruby 1.8.7+ for older ruby.
@@ -178,6 +180,7 @@ test_files:
178
180
  - test/method_test.rb
179
181
  - test/module_test.rb
180
182
  - test/object_test.rb
183
+ - test/proc_test.rb
181
184
  - test/regexp_test.rb
182
185
  - test/string_test.rb
183
186
  - test/symbol_test.rb