Commit ca1bc529 authored by Jun Hiroe's avatar Jun Hiroe

Add to_enum unless block

parent 03fa4576
......@@ -25,15 +25,20 @@ module Enumerable
##
# call-seq:
# enum.drop_while {|arr| block } -> array
# enum.drop_while -> an_enumerator
#
# Drops elements up to, but not including, the first element for
# which the block returns +nil+ or +false+ and returns an array
# containing the remaining elements.
#
# If no block is given, an enumerator is returned instead.
#
# a = [1, 2, 3, 4, 5, 0]
# a.drop_while {|i| i < 3 } #=> [3, 4, 5, 0]
def drop_while(&block)
return to_enum :drop_while unless block_given?
ary, state = [], false
self.each do |*val|
state = true if !state and !block.call(*val)
......@@ -67,15 +72,19 @@ module Enumerable
##
# call-seq:
# enum.take_while {|arr| block } -> array
# enum.take_while -> an_enumerator
#
# Passes elements to the block until the block returns +nil+ or +false+,
# then stops iterating and returns an array of all prior elements.
#
# If no block is given, an enumerator is returned instead.
#
# a = [1, 2, 3, 4, 5, 0]
# a.take_while {|i| i < 3 } #=> [1, 2]
def take_while(&block)
return to_enum :take_while unless block_given?
ary = []
self.each do |*val|
return ary unless block.call(*val)
......@@ -149,6 +158,7 @@ module Enumerable
##
# call-seq:
# enum.group_by {| obj | block } -> a_hash
# enum.group_by -> an_enumerator
#
# Returns a hash, which keys are evaluated result from the
# block, and values are arrays of elements in <i>enum</i>
......@@ -157,6 +167,8 @@ module Enumerable
# (1..6).group_by {|i| i%3} #=> {0=>[3, 6], 1=>[1, 4], 2=>[2, 5]}
def group_by(&block)
return to_enum :group_by unless block_given?
h = {}
self.each do |*val|
key = block.call(*val)
......@@ -169,10 +181,16 @@ module Enumerable
##
# call-seq:
# enum.sort_by { |obj| block } -> array
# enum.sort_by -> an_enumerator
#
# Sorts <i>enum</i> using a set of keys generated by mapping the
# values in <i>enum</i> through the given block.
#
# If no block is given, an enumerator is returned instead.
def sort_by(&block)
return to_enum :sort_by unless block_given?
ary = []
orig = []
self.each_with_index{|e, i|
......@@ -393,6 +411,8 @@ module Enumerable
# %w(albatross dog horse).minmax_by { |x| x.length } #=> ["dog", "albatross"]
def minmax_by(&block)
return to_enum :minmax_by unless block_given?
max = nil
max_cmp = nil
min = nil
......@@ -522,6 +542,8 @@ module Enumerable
#
def reverse_each(&block)
return to_enum :reverse_each unless block_given?
ary = self.to_a
i = ary.size - 1
while i>=0
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment