In Files

  • mrbgems/mruby-enum-ext/mrblib/enum.rb
  • mrbgems/mruby-enum-lazy/mrblib/lazy.rb
  • mrbgems/mruby-enum-lazy/test/lazy.rb
  • mrbgems/mruby-enumerator/mrblib/enumerator.rb
  • mrblib/enum.rb

Enumerable

Enumerable

#lazy implementation

#lazy returns an instance of Enumerable::Lazy. You can use it just like as normal Enumerable object, except these methods act as 'lazy':

- map       collect
- select    find_all
- reject
- grep
- drop
- drop_while
- take_while
- flat_map  collect_concat
- zip

Acknowledgements

Based on https://github.com/yhara/enumerable-lazy
Inspired by https://github.com/antimon2/enumerable_lz
http://jp.rubyist.net/magazine/?0034-Enumerable_lz (ja)

Enumerable

ISO 15.3.2

The <code>Enumerable</code> mixin provides collection classes with
several traversal and searching methods, and with the ability to
sort. The class must provide a method <code>each</code>, which
yields successive members of the collection. If
<code>Enumerable#max</code>, <code>#min</code>, or
<code>#sort</code> is used, the objects in the collection must also
implement a meaningful <code><=></code> operator, as these methods
rely on an ordering between members of the collection.

Constants

NONE

Public Instance Methods

__sort_sub__(sorted, work, src_ary, head, tail, &block) click to toggle source

TODO Does this OK? Please test it.

 
               # File mrblib/enum.rb, line 342
def __sort_sub__(sorted, work, src_ary, head, tail, &block)
  if head == tail
    sorted[head] = work[head] if src_ary == 1
    return
  end

  # on current step, which is a src ary?
  if src_ary == 0
    src, dst = sorted, work
  else
    src, dst = work, sorted
  end

  key = src[head]    # key value for dividing values
  i, j = head, tail  # position to store on the dst ary

  (head + 1).upto(tail){|idx|
    if ((block)? block.call(src[idx], key): (src[idx] <=> key)) > 0
      # larger than key
      dst[j] = src[idx]
      j -= 1
    else
      dst[i] = src[idx]
      i += 1
    end
  }

  sorted[i] = key

  # sort each sub-array
  src_ary = (src_ary + 1) % 2  # exchange a src ary
  __sort_sub__(sorted, work, src_ary, head, i - 1, &block) if i > head
  __sort_sub__(sorted, work, src_ary, i + 1, tail, &block) if i < tail
end
            
all?(&block) click to toggle source

Call the given block for each element which is yield by each. Return false if one block value is false. Otherwise return true. If no block is given and self is false return false.

ISO 15.3.2.2.1

 
               # File mrblib/enum.rb, line 25
def all?(&block)
  if block
    self.each{|*val|
      unless block.call(*val)
        return false
      end
    }
  else
    self.each{|*val|
      unless val.__svalue
        return false
      end
    }
  end
  true
end
            
any?(&block) click to toggle source

Call the given block for each element which is yield by each. Return true if one block value is true. Otherwise return false. If no block is given and self is true object return true.

ISO 15.3.2.2.2

 
               # File mrblib/enum.rb, line 50
def any?(&block)
  if block
    self.each{|*val|
      if block.call(*val)
        return true
      end
    }
  else
    self.each{|*val|
      if val.__svalue
        return true
      end
    }
  end
  false
end
            
collect(&block) click to toggle source

Call the given block for each element which is yield by each. Append all values of each block together and return this value.

ISO 15.3.2.2.3

 
               # File mrblib/enum.rb, line 74
def collect(&block)
  return to_enum :collect unless block_given?

  ary = []
  self.each{|*val|
    ary.push(block.call(*val))
  }
  ary
end
            
Also aliased as: map
collect_concat(&block) click to toggle source
Alias for: flat_map
count → int click to toggle source
count(item) → int
count { |obj| block } → int

Returns the number of items in enum through enumeration. If an argument is given, the number of items in enum that are equal to item are counted. If a block is given, it counts the number of elements yielding a true value.

 
               # File mrbgems/mruby-enum-ext/mrblib/enum.rb, line 245
def count(v=NONE, &block)
  count = 0
  if block
    self.each do |*val|
      count += 1 if block.call(*val)
    end
  else
    if v == NONE
      self.each { count += 1 }
    else
      self.each do |*val|
        count += 1 if val.__svalue == v 
      end
    end
  end
  count
end
            
cycle(n=nil) { |obj| block } → nil click to toggle source
cycle(n=nil) → an_enumerator

Calls block for each element of enum repeatedly n times or forever if none or nil is given. If a non-positive number is given or the collection is empty, does nothing. Returns nil if the loop has finished without getting interrupted.

#cycle saves elements in an internal array so changes to enum after the first pass have no effect.

If no block is given, an enumerator is returned instead.

a = ["a", "b", "c"]
a.cycle { |x| puts x }  # print, a, b, c, a, b, c,.. forever.
a.cycle(2) { |x| puts x }  # print, a, b, c, a, b, c.
 
               # File mrbgems/mruby-enum-ext/mrblib/enum.rb, line 576
def cycle(n=nil, &block)
  return to_enum(:cycle, n) if !block_given? && n == nil

  ary = []
  if n == nil
    self.each do|*val|
      ary.push val
      block.call(*val)
    end
    loop do
      ary.each do|e|
        block.call(*e)
      end
    end
  else
    raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)

    n = n.to_int
    self.each do|*val|
      ary.push val
    end
    count = 0
    while count < n
      ary.each do|e|
        block.call(*e)
      end
      count += 1
    end
  end
end
            
detect(ifnone=nil, &block) click to toggle source

Call the given block for each element which is yield by each. Return ifnone if no block value was true. Otherwise return the first block value which had was true.

ISO 15.3.2.2.4

 
               # File mrblib/enum.rb, line 92
def detect(ifnone=nil, &block)
  ret = ifnone
  self.each{|*val|
    if block.call(*val)
      ret = val.__svalue
      break
    end
  }
  ret
end
            
Also aliased as: find
drop(n) → array click to toggle source

Drops first n elements from enum, and returns rest elements in an array.

a = [1, 2, 3, 4, 5, 0]
a.drop(3)             #=> [4, 5, 0]
 
               # File mrbgems/mruby-enum-ext/mrblib/enum.rb, line 15
def drop(n)
  raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)
  raise ArgumentError, "attempt to drop negative size" if n < 0

  n = n.to_int
  ary = []
  self.each {|*val| n == 0 ? ary << val.__svalue : n -= 1 }
  ary
end
            
drop_while {|arr| block } → array click to toggle source
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]
 
               # File mrbgems/mruby-enum-ext/mrblib/enum.rb, line 39
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)
    ary << val.__svalue if state
  end
  ary
end
            
each_cons(n) {...} → nil click to toggle source

Iterates the given block for each array of consecutive <n> elements.

e.g.:

(1..10).each_cons(3) {|a| p a}
# outputs below
[1, 2, 3]
[2, 3, 4]
[3, 4, 5]
[4, 5, 6]
[5, 6, 7]
[6, 7, 8]
[7, 8, 9]
[8, 9, 10]
 
               # File mrbgems/mruby-enum-ext/mrblib/enum.rb, line 115
def each_cons(n, &block)
  raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)
  raise ArgumentError, "invalid size" if n <= 0

  ary = []
  n = n.to_int
  self.each do |*val|
    ary.shift if ary.size == n
    ary << val.__svalue
    block.call(ary.dup) if ary.size == n
  end
end
            
each_slice(n) {...} → nil click to toggle source

Iterates the given block for each slice of <n> elements.

e.g.:

(1..10).each_slice(3) {|a| p a}
# outputs below
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
[10]
 
               # File mrbgems/mruby-enum-ext/mrblib/enum.rb, line 142
def each_slice(n, &block)
  raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)
  raise ArgumentError, "invalid slice size" if n <= 0

  ary = []
  n = n.to_int
  self.each do |*val|
    ary << val.__svalue
    if ary.size == n
      block.call(ary)
      ary = []
    end
  end
  block.call(ary) unless ary.empty?
end
            
each_with_index(&block) click to toggle source

Call the given block for each element which is yield by each. Pass an index to the block which starts at 0 and increase by 1 for each element.

ISO 15.3.2.2.5

 
               # File mrblib/enum.rb, line 110
def each_with_index(&block)
  return to_enum :each_with_index unless block_given?

  i = 0
  self.each{|*val|
    block.call(val.__svalue, i)
    i += 1
  }
  self
end
            
each_with_object(obj) { |(*args), memo_obj| ... } → obj click to toggle source
each_with_object(obj) → an_enumerator

Iterates the given block for each element with an arbitrary object given, and returns the initially given object.

If no block is given, returns an enumerator.

(1..10).each_with_object([]) { |i, a| a << i*2 }
#=> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
 
               # File mrbgems/mruby-enum-ext/mrblib/enum.rb, line 517
def each_with_object(obj=nil, &block)
  raise ArgumentError, "wrong number of arguments (0 for 1)" if obj == nil

  return to_enum(:each_with_object, obj) unless block_given?

  self.each {|*val| block.call(val.__svalue, obj) }
  obj
end
            
entries() click to toggle source

Return an array of all elements which are yield by each.

ISO 15.3.2.2.6

 
               # File mrblib/enum.rb, line 126
def entries
  ary = []
  self.each{|*val|
    # __svalue is an internal method
    ary.push val.__svalue
  }
  ary
end
            
Also aliased as: to_a
find(ifnone=nil, &block) click to toggle source

Alias for find

ISO 15.3.2.2.7

Alias for: detect
find_all(&block) click to toggle source

Call the given block for each element which is yield by each. Return an array which contains all elements whose block value was true.

ISO 15.3.2.2.8

 
               # File mrblib/enum.rb, line 148
def find_all(&block)
  return to_enum :find_all unless block_given?

  ary = []
  self.each{|*val|
    ary.push(val.__svalue) if block.call(*val)
  }
  ary
end
            
Also aliased as: select
find_index(value) → int or nil click to toggle source
find_index { |obj| block } → int or nil
find_index → an_enumerator

Compares each entry in enum with value or passes to block. Returns the index for the first for which the evaluated value is non-false. If no object matches, returns nil

If neither block nor argument is given, an enumerator is returned instead.

(1..10).find_index  { |i| i % 5 == 0 and i % 7 == 0 }  #=> nil
(1..100).find_index { |i| i % 5 == 0 and i % 7 == 0 }  #=> 34
(1..100).find_index(50)                                #=> 49
 
               # File mrbgems/mruby-enum-ext/mrblib/enum.rb, line 625
def find_index(val=NONE, &block)
  return to_enum(:find_index, val) if !block_given? && val == NONE

  idx = 0
  if block
    self.each do |*e|
      return idx if block.call(*e)
      idx += 1
    end
  else
    self.each do |*e|
      return idx if e.__svalue == val
      idx += 1
    end
  end
  nil
end
            
first → obj or nil click to toggle source
first(n) → an_array

Returns the first element, or the first n elements, of the enumerable. If the enumerable is empty, the first form returns nil, and the second form returns an empty array.

 
               # File mrbgems/mruby-enum-ext/mrblib/enum.rb, line 217
def first(n=NONE)
  if n == NONE
    self.each do |*val|
      return val.__svalue
    end
    return nil
  else
    a = []
    i = 0
    self.each do |*val|
      break if n<=i
      a.push val.__svalue
      i += 1
    end
    a
  end
end
            
flat_map { |obj| block } → array click to toggle source
collect_concat { |obj| block } → array
flat_map → an_enumerator
collect_concat → an_enumerator

Returns a new array with the concatenated results of running block once for every element in enum.

If no block is given, an enumerator is returned instead.

[1, 2, 3, 4].flat_map { |e| [e, -e] } #=> [1, -1, 2, -2, 3, -3, 4, -4]
[[1, 2], [3, 4]].flat_map { |e| e + [100] } #=> [1, 2, 100, 3, 4, 100]
 
               # File mrbgems/mruby-enum-ext/mrblib/enum.rb, line 277
def flat_map(&block)
  return to_enum :flat_map unless block_given?

  ary = []
  self.each do |*e|
    e2 = block.call(*e)
    if e2.respond_to? :each
      e2.each {|e3| ary.push(e3) }
    else
      ary.push(e2)
    end
  end
  ary
end
            
Also aliased as: collect_concat
grep(pattern, &block) click to toggle source

Call the given block for each element which is yield by each and which return value was true when invoking === with pattern. Return an array with all elements or the respective block values.

ISO 15.3.2.2.9

 
               # File mrblib/enum.rb, line 166
def grep(pattern, &block)
  ary = []
  self.each{|*val|
    sv = val.__svalue
    if pattern === sv
      ary.push((block)? block.call(*val): sv)
    end
  }
  ary
end
            
group_by {| obj | block } → a_hash click to toggle source
group_by → an_enumerator

Returns a hash, which keys are evaluated result from the block, and values are arrays of elements in enum corresponding to the key.

(1..6).group_by {|i| i%3}   #=> {0=>[3, 6], 1=>[1, 4], 2=>[2, 5]}
 
               # File mrbgems/mruby-enum-ext/mrblib/enum.rb, line 169
def group_by(&block)
  return to_enum :group_by unless block_given?

  h = {}
  self.each do |*val|
    key = block.call(*val)
    sv = val.__svalue
    h.key?(key) ? (h[key] << sv) : (h[key] = [sv])
  end
  h
end
            
hash() click to toggle source

redefine hash 15.3.1.3.15

 
               # File mrblib/enum.rb, line 403
def hash
  h = 12347
  self.each do |e|
    h ^= e.hash
  end
  h
end
            
include?(obj) click to toggle source

Return true if at least one element which is yield by each returns a true value by invoking == with obj. Otherwise return false.

ISO 15.3.2.2.10

 
               # File mrblib/enum.rb, line 184
def include?(obj)
  self.each{|*val|
    if val.__svalue == obj
      return true
    end
  }
  false
end
            
Also aliased as: member?
inject(*args, &block) click to toggle source

Call the given block for each element which is yield by each. Return value is the sum of all block values. Pass to each block the current sum and the current element.

ISO 15.3.2.2.11

 
               # File mrblib/enum.rb, line 201
def inject(*args, &block)
  raise ArgumentError, "too many arguments" if args.size > 2
  if Symbol === args[-1]
    sym = args[-1]
    block = ->(x,y){x.send(sym,y)}
    args.pop
  end
  if args.empty?
    flag = true  # no initial argument
    result = nil
  else
    flag = false
    result = args[0]
  end
  self.each{|*val|
    val = val.__svalue
    if flag
      # push first element as initial
      flag = false
      result = val
    else
      result = block.call(result, val)
    end
  }
  result
end
            
Also aliased as: reduce
lazy() click to toggle source
 
               # File mrbgems/mruby-enum-lazy/mrblib/lazy.rb, line 24
def lazy
  Lazy.new(self)
end
            
map(&block) click to toggle source

Alias for collect

ISO 15.3.2.2.12

Alias for: collect
max(&block) click to toggle source

Return the maximum value of all elements yield by each. If no block is given <=> will be invoked to define this value. If a block is given it will be used instead.

ISO 15.3.2.2.13

 
               # File mrblib/enum.rb, line 242
def max(&block)
  flag = true  # 1st element?
  result = nil
  self.each{|*val|
    val = val.__svalue
    if flag
      # 1st element
      result = val
      flag = false
    else
      if block
        result = val if block.call(val, result) > 0
      else
        result = val if (val <=> result) > 0
      end
    end
  }
  result
end
            
max_by {|obj| block } → obj click to toggle source
max_by → an_enumerator

Returns the object in enum that gives the maximum value from the given block.

If no block is given, an enumerator is returned instead.

%w[albatross dog horse].max_by {|x| x.length }   #=> "albatross"
 
               # File mrbgems/mruby-enum-ext/mrblib/enum.rb, line 305
def max_by(&block)
  return to_enum :max_by unless block_given?

  first = true
  max = nil
  max_cmp = nil

  self.each do |*val|
    if first
      max = val.__svalue
      max_cmp = block.call(*val)
      first = false
    else
      if (cmp = block.call(*val)) > max_cmp
        max = val.__svalue
        max_cmp = cmp
      end
    end
  end
  max
end
            
member?(obj) click to toggle source

Alias for include?

ISO 15.3.2.2.15

Alias for: include?
min(&block) click to toggle source

Return the minimum value of all elements yield by each. If no block is given <=> will be invoked to define this value. If a block is given it will be used instead.

ISO 15.3.2.2.14

 
               # File mrblib/enum.rb, line 269
def min(&block)
  flag = true  # 1st element?
  result = nil
  self.each{|*val|
    val = val.__svalue
    if flag
      # 1st element
      result = val
      flag = false
    else
      if block
        result = val if block.call(val, result) < 0
      else
        result = val if (val <=> result) < 0
      end
    end
  }
  result
end
            
min_by {|obj| block } → obj click to toggle source
min_by → an_enumerator

Returns the object in enum that gives the minimum value from the given block.

If no block is given, an enumerator is returned instead.

%w[albatross dog horse].min_by {|x| x.length }   #=> "dog"
 
               # File mrbgems/mruby-enum-ext/mrblib/enum.rb, line 339
def min_by(&block)
  return to_enum :min_by unless block_given?

  first = true
  min = nil
  min_cmp = nil

  self.each do |*val|
    if first
      min = val.__svalue
      min_cmp = block.call(*val)
      first = false
    else
      if (cmp = block.call(*val)) < min_cmp
        min = val.__svalue
        min_cmp = cmp
      end
    end
  end
  min
end
            
minmax → [min, max] click to toggle source
minmax { |a, b| block } → [min, max]

Returns two elements array which contains the minimum and the maximum value in the enumerable. The first form assumes all objects implement Comparable; the second uses the block to return a <=> b.

a = %w(albatross dog horse)
a.minmax                                  #=> ["albatross", "horse"]
a.minmax { |a, b| a.length <=> b.length } #=> ["dog", "albatross"]
 
               # File mrbgems/mruby-enum-ext/mrblib/enum.rb, line 375
def minmax(&block)
  max = nil
  min = nil
  first = true

  self.each do |*val|
    if first
      val = val.__svalue
      max = val
      min = val
      first = false
    else
      if block
        max = val.__svalue if block.call(*val, max) > 0
        min = val.__svalue if block.call(*val, min) < 0
      else
        val = val.__svalue
        max = val if (val <=> max) > 0
        min = val if (val <=> min) < 0
      end
    end
  end
  [min, max]
end
            
minmax_by { |obj| block } → [min, max] click to toggle source
minmax_by → an_enumerator

Returns a two element array containing the objects in enum that correspond to the minimum and maximum values respectively from the given block.

If no block is given, an enumerator is returned instead.

%w(albatross dog horse).minmax_by { |x| x.length }   #=> ["dog", "albatross"]
 
               # File mrbgems/mruby-enum-ext/mrblib/enum.rb, line 413
def minmax_by(&block)
  return to_enum :minmax_by unless block_given?

  max = nil
  max_cmp = nil
  min = nil
  min_cmp = nil
  first = true

  self.each do |*val|
    if first
      max = min = val.__svalue
      max_cmp = min_cmp = block.call(*val)
      first = false
   else
      if (cmp = block.call(*val)) > max_cmp
        max = val.__svalue
        max_cmp = cmp
      end
      if (cmp = block.call(*val)) < min_cmp
        min = val.__svalue
        min_cmp = cmp
      end
    end
  end
  [min, max]
end
            
none? [{ |obj| block }] → true or false click to toggle source

Passes each element of the collection to the given block. The method returns true if the block never returns true for all elements. If the block is not given, none? will return true only if none of the collection members is true.

%w(ant bear cat).none? { |word| word.length == 5 } #=> true
%w(ant bear cat).none? { |word| word.length >= 4 } #=> false
[].none?                                           #=> true
[nil, false].none?                                 #=> true
[nil, true].none?                                  #=> false
 
               # File mrbgems/mruby-enum-ext/mrblib/enum.rb, line 456
def none?(&block)
  if block
    self.each do |*val|
      return false if block.call(*val)
    end
  else
    self.each do |*val|
      return false if val.__svalue
    end
  end
  true
end
            
one? [{ |obj| block }] → true or false click to toggle source

Passes each element of the collection to the given block. The method returns true if the block returns true exactly once. If the block is not given, one? will return true only if exactly one of the collection members is true.

%w(ant bear cat).one? { |word| word.length == 4 }  #=> true
%w(ant bear cat).one? { |word| word.length > 4 }   #=> false
%w(ant bear cat).one? { |word| word.length < 4 }   #=> false
[nil, true, 99].one?                               #=> false
[nil, true, false].one?                            #=> true
 
               # File mrbgems/mruby-enum-ext/mrblib/enum.rb, line 486
def one?(&block)
  count = 0
  if block
    self.each do |*val|
      count += 1 if block.call(*val)
      return false if count > 1
    end
  else
    self.each do |*val|
      count += 1 if val.__svalue
      return false if count > 1
    end
  end

  count == 1 ? true : false
end
            
partition(&block) click to toggle source

Call the given block for each element which is yield by each. Return an array which contains two arrays. The first array contains all elements whose block value was true. The second array contains all elements whose block value was false.

ISO 15.3.2.2.16

 
               # File mrblib/enum.rb, line 305
def partition(&block)
  ary_T = []
  ary_F = []
  self.each{|*val|
    if block.call(*val)
      ary_T.push(val.__svalue)
    else
      ary_F.push(val.__svalue)
    end
  }
  [ary_T, ary_F]
end
            
reduce(*args, &block) click to toggle source
Alias for: inject
reject(&block) click to toggle source

Call the given block for each element which is yield by each. Return an array which contains only the elements whose block value was false.

ISO 15.3.2.2.17

 
               # File mrblib/enum.rb, line 325
def reject(&block)
  ary = []
  self.each{|*val|
    ary.push(val.__svalue) unless block.call(*val)
  }
  ary
end
            
reverse_each { |item| block } → enum click to toggle source
reverse_each → an_enumerator

Builds a temporary array and traverses that array in reverse order.

If no block is given, an enumerator is returned instead.

  (1..3).reverse_each { |v| p v }

produces:

  3
  2
  1
 
               # File mrbgems/mruby-enum-ext/mrblib/enum.rb, line 544
def reverse_each(&block)
  return to_enum :reverse_each unless block_given?

  ary = self.to_a
  i = ary.size - 1
  while i>=0
    block.call(ary[i])
    i -= 1
  end
  self
end
            
select(&block) click to toggle source

Alias for find_all.

ISO 15.3.2.2.18

Alias for: find_all
sort(&block) click to toggle source

Return a sorted array of all elements which are yield by each. If no block is given <=> will be invoked on each element to define the order. Otherwise the given block will be used for sorting.

ISO 15.3.2.2.19

 
               # File mrblib/enum.rb, line 387
def sort(&block)
  ary = []
  self.each{|*val| ary.push(val.__svalue)}
  if ary.size > 1
    __sort_sub__(ary, ::Array.new(ary.size), 0, 0, ary.size - 1, &block)
  end
  ary
end
            
sort_by { |obj| block } → array click to toggle source
sort_by → an_enumerator

Sorts enum using a set of keys generated by mapping the values in enum through the given block.

If no block is given, an enumerator is returned instead.

 
               # File mrbgems/mruby-enum-ext/mrblib/enum.rb, line 191
def sort_by(&block)
  return to_enum :sort_by unless block_given?

  ary = []
  orig = [] 
  self.each_with_index{|e, i|
    orig.push(e)
    ary.push([block.call(e), i])
  }
  if ary.size > 1
    __sort_sub__(ary, ::Array.new(ary.size), 0, 0, ary.size - 1) do |a,b|
      a <=> b
    end
  end
  ary.collect{|e,i| orig[i]}
end
            
take(n) → array click to toggle source

Returns first n elements from enum.

a = [1, 2, 3, 4, 5, 0]
a.take(3)             #=> [1, 2, 3]
 
               # File mrbgems/mruby-enum-ext/mrblib/enum.rb, line 59
def take(n)
  raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)
  raise ArgumentError, "attempt to take negative size" if n < 0

  n = n.to_int
  ary = []
  self.each do |*val|
    break if ary.size >= n
    ary << val.__svalue
  end
  ary
end
            
take_while {|arr| block } → array click to toggle source
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]
 
               # File mrbgems/mruby-enum-ext/mrblib/enum.rb, line 85
def take_while(&block)
  return to_enum :take_while unless block_given?

  ary = []
  self.each do |*val|
    return ary unless block.call(*val)
    ary << val.__svalue
  end
  ary
end
            
to_a() click to toggle source

Alias for entries.

ISO 15.3.2.2.20

Alias for: entries
zip(arg, ...) → an_array_of_array click to toggle source

Takes one element from enum and merges corresponding elements from each args. This generates a sequence of n-element arrays, where n is one more than the count of arguments. The length of the resulting sequence will be enum#size. If the size of any argument is less than enum#size, nil values are supplied.

 
               # File mrbgems/mruby-enum-ext/mrblib/enum.rb, line 655
def zip(*arg)
  ary = []
  arg = arg.map{|a|a.to_a}
  i = 0
  self.each do |*val|
    a = []
    a.push(val.__svalue)
    idx = 0
    while idx < arg.size
      a.push(arg[idx][i])
      idx += 1
    end
    ary.push(a)
    i += 1
  end
  ary
end
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus