summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-enum-ext
diff options
context:
space:
mode:
authorJun Hiroe <[email protected]>2014-03-28 18:31:30 +0900
committerJun Hiroe <[email protected]>2014-03-28 18:54:25 +0900
commitca1bc5290e05379dc128a2a1627f55c697f791e1 (patch)
tree85b43a508970300c1a71b5ebe22ed6573ad99295 /mrbgems/mruby-enum-ext
parent03fa45760e2da4c09acb725fdca20f4e8f7adb27 (diff)
downloadmruby-ca1bc5290e05379dc128a2a1627f55c697f791e1.tar.gz
mruby-ca1bc5290e05379dc128a2a1627f55c697f791e1.zip
Add to_enum unless block
Diffstat (limited to 'mrbgems/mruby-enum-ext')
-rw-r--r--mrbgems/mruby-enum-ext/mrblib/enum.rb22
1 files changed, 22 insertions, 0 deletions
diff --git a/mrbgems/mruby-enum-ext/mrblib/enum.rb b/mrbgems/mruby-enum-ext/mrblib/enum.rb
index 2affe555f..0a18a3d35 100644
--- a/mrbgems/mruby-enum-ext/mrblib/enum.rb
+++ b/mrbgems/mruby-enum-ext/mrblib/enum.rb
@@ -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