Sha256: e6a8f1043cf1ad5aed5ff5230bfa25d5800d2ba06aec2c53de717a193b1f4b1f

Contents?: true

Size: 731 Bytes

Versions: 1

Compression:

Stored size: 731 Bytes

Contents

module Enumerable
  # Create a hash whose keys are the enumerable's elements, with specified
  # values.
  #
  # If no block is given, the given parameter (default true) is used for
  # all values, e.g.:
  #
  #     [1,2,3,4].hashify               --> {1=>true, 2=>true, 3=>true, 4=>true}
  #     [1,2,3,4].hashify("a")          --> {1=>"a", 2=>"a", 3=>"a", 4=>"a"}
  #
  # If a block is given, each key's value is the result of running the
  # block for that key, e.g.:
  #
  #     [1,2,3,4].hashify{|n| "a" * n}  --> {1=>"a", 2=>"aa", 3=>"aaa", 4=>"aaaa"}
  #
  def hashify(val=true)
    h = {}
    if block_given?
      each { |item| h[item] = yield(item) }
    else
      each { |item| h[item] = val }
    end
    h
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
enumerable_hashify-0.0.2 lib/enumerable_hashify/enumerable.rb