lib/polyfill/v2_2/enumerable.rb in polyfill-0.8.0 vs lib/polyfill/v2_2/enumerable.rb in polyfill-0.9.0

- old
+ new

@@ -1,7 +1,43 @@ module Polyfill module V2_2 module Enumerable + def max(n = nil) + return block_given? ? super(&::Proc.new) : super() if n.nil? || n == 1 + + raise ArgumentError, "negative size (#{n})" if n < 0 + + (block_given? ? sort(&::Proc.new) : sort).last(n).reverse + end + + def max_by(n = nil) + if n.nil? || n == 1 || !block_given? + return block_given? ? super(&::Proc.new) : super() + end + + raise ArgumentError, "negative size (#{n})" if n < 0 + + sort_by(&::Proc.new).last(n).reverse + end + + def min(n = nil) + return block_given? ? super(&::Proc.new) : super() if n.nil? || n == 1 + + raise ArgumentError, "negative size (#{n})" if n < 0 + + (block_given? ? sort(&::Proc.new) : sort).first(n) + end + + def min_by(n = nil) + if n.nil? || n == 1 || !block_given? + return block_given? ? super(&::Proc.new) : super() + end + + raise ArgumentError, "negative size (#{n})" if n < 0 + + sort_by(&::Proc.new).first(n) + end + def slice_after(pattern = nil) raise ArgumentError, 'both pattern and block are given' if pattern && block_given? raise ArgumentError, 'wrong number of arguments (given 0, expected 1)' if !pattern && !block_given? matcher = pattern || ::Proc.new