Skip to content

Commit

Permalink
[GR-20075] Implement Method#<< and Method#>> (#1821).
Browse files Browse the repository at this point in the history
PullRequest: truffleruby/1182
  • Loading branch information
bjfish committed Dec 12, 2019
2 parents 88a8287 + d55a827 commit 192c776
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Compatibility:
* Implemented `rb_hash_start` (#1841, @XrXr).
* JCodings has been updated from 1.0.42 to 1.0.45.
* Joni has been updated from 2.1.25 to 2.1.30.
* Implemented `Method#<<` and `Method#>>` (#1821).

Performance:

Expand Down
32 changes: 32 additions & 0 deletions spec/ruby/core/proc/compose_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ def double.call(n); n * 2; end
(f << g).lambda?.should == false
end

it "is a Proc when other is lambda" do
f = proc { |x| x * x }
g = ->(x) { x + x }

(f << g).is_a?(Proc).should == true
(f << g).lambda?.should == false
end

it "is a lambda when self is lambda" do
f = lambda { |x| x * x }
g = proc { |x| x + x }

(f << g).is_a?(Proc).should == true
(f << g).lambda?.should == true
end

it "may accept multiple arguments" do
inc = proc { |n| n + 1 }
mul = proc { |n, m| n * m }
Expand Down Expand Up @@ -91,6 +107,22 @@ def double.call(n); n * 2; end
(f >> g).lambda?.should == false
end

it "is a Proc when other is lambda" do
f = proc { |x| x * x }
g = ->(x) { x + x }

(f >> g).is_a?(Proc).should == true
(f >> g).lambda?.should == false
end

it "is a lambda when self is lambda" do
f = lambda { |x| x * x }
g = proc { |x| x + x }

(f >> g).is_a?(Proc).should == true
(f >> g).lambda?.should == true
end

it "may accept multiple arguments" do
inc = proc { |n| n + 1 }
mul = proc { |n, m| n * m }
Expand Down
14 changes: 0 additions & 14 deletions spec/tags/core/method/compose_tags.txt

This file was deleted.

8 changes: 8 additions & 0 deletions src/main/ruby/truffleruby/core/method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ def curry(curried_arity = nil)
self.to_proc.curry(curried_arity)
end

def >>(other)
self.to_proc >> other
end

def <<(other)
self.to_proc << other
end

alias_method :to_s, :inspect

end
4 changes: 2 additions & 2 deletions src/main/ruby/truffleruby/core/proc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def to_proc
end

def >>(other)
if other.respond_to?(:lambda?) && other.lambda?
if lambda?
-> (*args, &block) do
other.call(call(*args, &block))
end
Expand All @@ -117,7 +117,7 @@ def >>(other)
end

def <<(other)
if other.respond_to?(:lambda?) && other.lambda?
if lambda?
-> (*args, &block) do
call(other.call(*args, &block))
end
Expand Down

0 comments on commit 192c776

Please sign in to comment.