Skip to content

Commit 70722f6

Browse files
committed
[GR-16907] Implement Proc#<< and #>>.
PullRequest: truffleruby/924
2 parents b1ec3f4 + bc3beb9 commit 70722f6

File tree

5 files changed

+41
-20
lines changed

5 files changed

+41
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Compatibility:
1010
* Added `Kernel#then` (#1703).
1111
* `FFI::Struct#[]=` is now supported for inline character arrays.
1212
* `blocking: true` is now supported for `FFI::Library#attach_function`.
13+
* Implemented `Proc#>>` and `#<<` (#1688).
1314

1415
Changes:
1516

spec/ruby/core/proc/compose_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ def double.call(n); n * 2; end
4444

4545
(inc << mul).call(2, 3).should == 7
4646
end
47+
48+
it "passes blocks to the second proc" do
49+
ScratchPad.record []
50+
one = proc { |&arg| arg.call :one if arg }
51+
two = proc { |&arg| arg.call :two if arg }
52+
(one << two).call { |x| ScratchPad << x }
53+
ScratchPad.recorded.should == [:two]
54+
end
4755
end
4856
end
4957

@@ -89,6 +97,14 @@ def double.call(n); n * 2; end
8997

9098
(mul >> inc).call(2, 3).should == 7
9199
end
100+
101+
it "passes blocks to the first proc" do
102+
ScratchPad.record []
103+
one = proc { |&arg| arg.call :one if arg }
104+
two = proc { |&arg| arg.call :two if arg }
105+
(one >> two).call { |x| ScratchPad << x }
106+
ScratchPad.recorded.should == [:one]
107+
end
92108
end
93109
end
94110
end

spec/tags/core/proc/compose_tags.txt

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

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,28 @@ def to_s
103103
def to_proc
104104
self
105105
end
106+
107+
def >>(other)
108+
if other.respond_to?(:lambda?) && other.lambda?
109+
-> (*args, &block) do
110+
other.call(call(*args, &block))
111+
end
112+
else
113+
proc do |*args, &block|
114+
other.call(call(*args, &block))
115+
end
116+
end
117+
end
118+
119+
def <<(other)
120+
if other.respond_to?(:lambda?) && other.lambda?
121+
-> (*args, &block) do
122+
call(other.call(*args, &block))
123+
end
124+
else
125+
proc do |*args, &block|
126+
call(other.call(*args, &block))
127+
end
128+
end
129+
end
106130
end

test/mri/excludes/TestProc.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@
99
exclude :test_proc_args_opt_single, "needs investigation"
1010
exclude :test_to_s, "needs investigation"
1111
exclude :test_binding, "needs investigation"
12-
exclude :test_compose, "needs investigation"
13-
exclude :test_compose_with_block, "needs investigation"
14-
exclude :test_compose_with_callable, "needs investigation"
15-
exclude :test_compose_with_lambda, "needs investigation"
16-
exclude :test_compose_with_method, "needs investigation"
17-
exclude :test_compose_with_multiple_args, "needs investigation"
1812
exclude :test_curry_given_blocks, "needs investigation"
1913
exclude :test_curry_lambda_splat, "needs investigation"
2014
exclude :test_dup_clone, "needs investigation"

0 commit comments

Comments
 (0)