Sha256: 48f79a671f9eb86dc728c266e5de1ab1792ce717a16256a06f06394d96f786b0

Contents?: true

Size: 712 Bytes

Versions: 2

Compression:

Stored size: 712 Bytes

Contents

class Array
  def to_h
    map { |e| yield e }.inject({}) { |carry, e| carry.merge! e }
  end

  def strip_each
    self.map{|t| t.strip}
  end
  def strip_each!
    self.each{|t| t.strip!}
  end

  def to_sentence
    i = size
    if size<=2
      self.join(" and ")
    else
      [self[0..size-2].join(", "), self[size-1]].join(" and ")
    end
  end

  def map_with_index
    result = []
    self.each_with_index do |elt, idx|
      result << yield(elt, idx)
    end
    result
  end
  def plus_if(obj)
    self << obj unless include?(obj) || obj.nil?
  end

  def strip_all
    map!{|t| t.strip}
  end
end

module ArrayPlus
  def +(y)
    y.each do |o|
      self << o unless include?(o)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
da_huangs_ruby_extensions-0.0.4 lib/array.rb
da_huangs_ruby_extensions-0.0.1 lib/array.rb