Skip to content

Commit 192c776

Browse files
committed
[GR-20075] Implement Method#<< and Method#>> (#1821).
PullRequest: truffleruby/1182
2 parents 88a8287 + d55a827 commit 192c776

File tree

5 files changed

+43
-16
lines changed

5 files changed

+43
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Compatibility:
5252
* Implemented `rb_hash_start` (#1841, @XrXr).
5353
* JCodings has been updated from 1.0.42 to 1.0.45.
5454
* Joni has been updated from 2.1.25 to 2.1.30.
55+
* Implemented `Method#<<` and `Method#>>` (#1821).
5556

5657
Performance:
5758

spec/ruby/core/proc/compose_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@ def double.call(n); n * 2; end
3838
(f << g).lambda?.should == false
3939
end
4040

41+
it "is a Proc when other is lambda" do
42+
f = proc { |x| x * x }
43+
g = ->(x) { x + x }
44+
45+
(f << g).is_a?(Proc).should == true
46+
(f << g).lambda?.should == false
47+
end
48+
49+
it "is a lambda when self is lambda" do
50+
f = lambda { |x| x * x }
51+
g = proc { |x| x + x }
52+
53+
(f << g).is_a?(Proc).should == true
54+
(f << g).lambda?.should == true
55+
end
56+
4157
it "may accept multiple arguments" do
4258
inc = proc { |n| n + 1 }
4359
mul = proc { |n, m| n * m }
@@ -91,6 +107,22 @@ def double.call(n); n * 2; end
91107
(f >> g).lambda?.should == false
92108
end
93109

110+
it "is a Proc when other is lambda" do
111+
f = proc { |x| x * x }
112+
g = ->(x) { x + x }
113+
114+
(f >> g).is_a?(Proc).should == true
115+
(f >> g).lambda?.should == false
116+
end
117+
118+
it "is a lambda when self is lambda" do
119+
f = lambda { |x| x * x }
120+
g = proc { |x| x + x }
121+
122+
(f >> g).is_a?(Proc).should == true
123+
(f >> g).lambda?.should == true
124+
end
125+
94126
it "may accept multiple arguments" do
95127
inc = proc { |n| n + 1 }
96128
mul = proc { |n, m| n * m }

spec/tags/core/method/compose_tags.txt

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/main/ruby/truffleruby/core/method.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ def curry(curried_arity = nil)
3030
self.to_proc.curry(curried_arity)
3131
end
3232

33+
def >>(other)
34+
self.to_proc >> other
35+
end
36+
37+
def <<(other)
38+
self.to_proc << other
39+
end
40+
3341
alias_method :to_s, :inspect
3442

3543
end

src/main/ruby/truffleruby/core/proc.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def to_proc
105105
end
106106

107107
def >>(other)
108-
if other.respond_to?(:lambda?) && other.lambda?
108+
if lambda?
109109
-> (*args, &block) do
110110
other.call(call(*args, &block))
111111
end
@@ -117,7 +117,7 @@ def >>(other)
117117
end
118118

119119
def <<(other)
120-
if other.respond_to?(:lambda?) && other.lambda?
120+
if lambda?
121121
-> (*args, &block) do
122122
call(other.call(*args, &block))
123123
end

0 commit comments

Comments
 (0)