Sha256: f91c94c29f4e30a1c74ae42c1e17212f99e4f81b9a76e0972313d9508c4a8a14

Contents?: true

Size: 1.33 KB

Versions: 52

Compression:

Stored size: 1.33 KB

Contents

module Enumerable

  def map_and_find(not_found=nil)
    each do |x|
      val = yield(x)
      return val if val
    end
    not_found
  end

  def map_with_index(res=[])
    each_with_index {|x, i| res << yield(x, i)}
    res
  end

  def build_hash(res={})
    each do |x|
      pair = block_given? ? yield(x) : x
      res[pair.first] = pair.last if pair
    end
    res
  end

  def map_hash(res={})
    each do |x|
      v = yield x
      res[x] = v
    end
    res
  end

  def rest
    self[1..-1] || []
  end

  class MultiSender

    undef_method(*(instance_methods.map{|m| m.to_s} - %w*__id__ __send__ object_id*))

    def initialize(enumerable, method)
      @enumerable = enumerable
      @method     = method
    end

    def method_missing(name, *args, &block)
      @enumerable.send(@method) { |x| x.send(name, *args, &block) }
    end

  end

  def *()
    MultiSender.new(self, :map)
  end

  def where
    MultiSender.new(self, :select)
  end

  def where_not
    MultiSender.new(self, :reject)
  end

  def drop_while
    drop = 0
    drop += 1 while yield(self[drop])
    self[drop..-1]
  end


  def take_while
    take = 0
    take += 1 while yield(self[take])
    self[0..take-1]
  end

end


class Object

  def in?(enum)
    !enum.nil? && enum.include?(self)
  end

  def not_in?(enum)
    enum.nil? || !enum.include?(self)
  end

end

Version data entries

52 entries across 52 versions & 2 rubygems

Version Path
hobo_support-1.3.3 lib/hobo_support/enumerable.rb
hobo_support-1.3.2 lib/hobo_support/enumerable.rb
hobo_support-1.3.1 lib/hobo_support/enumerable.rb
hobo_support-1.4.0.pre8 lib/hobo_support/enumerable.rb
hobo_support-1.4.0.pre7 lib/hobo_support/enumerable.rb
hobo_support-1.4.0.pre6 lib/hobo_support/enumerable.rb
hobo_support-1.4.0.pre5 lib/hobo_support/enumerable.rb
hobo_support-1.4.0.pre4 lib/hobo_support/enumerable.rb
hobo_support-1.4.0.pre3 lib/hobo_support/enumerable.rb
hobo_support-1.4.0.pre2 lib/hobo_support/enumerable.rb
hobosupport-1.1.0 lib/hobo_support/enumerable.rb
hobo_support-1.3.0 lib/hobo_support/enumerable.rb
hobo_support-1.3.0.RC4 lib/hobo_support/enumerable.rb
hobo_support-1.3.0.RC3 lib/hobo_support/enumerable.rb
hobo_support-1.3.0.RC2 lib/hobo_support/enumerable.rb
hobo_support-1.3.0.RC1 lib/hobo_support/enumerable.rb
hobo_support-1.3.0.RC lib/hobo_support/enumerable.rb
hobo_support-1.3.0.pre31 lib/hobo_support/enumerable.rb
hobo_support-1.3.0.pre29 lib/hobo_support/enumerable.rb
hobo_support-1.3.0.pre28 lib/hobo_support/enumerable.rb