Skip to content

Commit

Permalink
[Truffle] Implement Enumerable#{mix,max} optional parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Fish committed Sep 4, 2016
1 parent d675b78 commit 294d80f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
4 changes: 0 additions & 4 deletions spec/truffle/tags/core/enumerable/max_tags.txt

This file was deleted.

4 changes: 0 additions & 4 deletions spec/truffle/tags/core/enumerable/min_tags.txt

This file was deleted.

30 changes: 28 additions & 2 deletions truffle/src/main/ruby/core/enumerable.rb
Expand Up @@ -647,7 +647,8 @@ def first(n=undefined)
nil
end

def min
def min(n = undefined, &block)
return min_n(n, &block) if !undefined.equal?(n) && !n.nil?
min = undefined
each do
o = Truffle.single_block_arg
Expand All @@ -667,10 +668,23 @@ def min

undefined.equal?(min) ? nil : min
end

def min_n(n, &block)
raise ArgumentError, "negative size #{n}" if n < 0
return [] if n == 0

if block_given?
self.sort(&block).first(n)
else
self.sort.first(n)
end

This comment has been minimized.

Copy link
@eregon

eregon Sep 6, 2016

Member

I think it would just work if &block is passed and is nil, no?

This comment has been minimized.

Copy link
@bjfish

bjfish Sep 7, 2016

Contributor

@eregon Yes, fixed at 3812700, thanks!

end
private :min_n

alias_method :min_internal, :min

def max
def max(n = undefined, &block)
return max_n(n, &block) if !undefined.equal?(n) && !n.nil?
max = undefined
each do
o = Truffle.single_block_arg
Expand All @@ -690,6 +704,18 @@ def max

undefined.equal?(max) ? nil : max
end

def max_n(n, &block)
raise ArgumentError, "negative size #{n}" if n < 0
return [] if n == 0

if block_given?
self.sort(&block).reverse.first(n)
else
self.sort.reverse.first(n)
end
end
private :max_n

alias_method :max_internal, :max

Expand Down

0 comments on commit 294d80f

Please sign in to comment.