Sha256: 3511327b3d8b08f3257dbe82f57a4b746da3960fe16004e16abf1fbff0f30b18

Contents?: true

Size: 1003 Bytes

Versions: 7

Compression:

Stored size: 1003 Bytes

Contents

# -*- coding: utf-8 -*-
module Enumerable

  # @example
  #   enum.select_while {|obj| block }
  #    => array
  #
  # @return [Array<Object>] the leading elements for which block is truthy.
  #
  def select_while
    arr = []
    each{|item| yield(item) ? (arr << item) : break}
    return arr
  end

  # @example
  #   enum.select_until {|obj| block }
  #    => array
  #
  # @return [Array<Object>] the leading elements for which block is falsey.
  #
  def select_until
    arr = []
    each{|item| yield(item) ? break : (arr << item)}
    return arr
  end

  # Calls block with two arguments, the item and its index, for each item in enum.
  #
  # @example
  #   enum.select_with_index {|obj,i| block }
  #   => array
  #
  # @return [Array<Object> the leading elements for which block is truthy.
  #
  def select_with_index
    index = 0
    arr = []
    each{|item|
      if yield(item,index)
        arr << item
        index+=1
      else
        break
      end
    }
    return arr
  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
sixarm_ruby_ramp-5.0.2 lib/sixarm_ruby_ramp/enumerable/select.rb
sixarm_ruby_ramp-5.0.1 lib/sixarm_ruby_ramp/enumerable/select.rb
sixarm_ruby_ramp-5.0.0 lib/sixarm_ruby_ramp/enumerable/select.rb
sixarm_ruby_ramp-4.2.7 lib/sixarm_ruby_ramp/enumerable/select.rb
sixarm_ruby_ramp-4.2.5 lib/sixarm_ruby_ramp/enumerable/select.rb
sixarm_ruby_ramp-4.2.4 lib/sixarm_ruby_ramp/enumerable/select.rb
sixarm_ruby_ramp-4.2.3 lib/sixarm_ruby_ramp/enumerable/select.rb