Sha256: e70e3b50f47c4d95faed94584a08fd4aaac754bfe494b00e5b65159f59ac81e2

Contents?: true

Size: 763 Bytes

Versions: 1

Compression:

Stored size: 763 Bytes

Contents

module Enumerable
  # If no block is given, returns a hash where each element in the enumrable is
  # a key whose value is the given parameter (default true).  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, returns a hash where each element in the enumerable is
  # a key whose value is the result of running the block for that element.
  # 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.1 lib/enumerable_hashify/enumerable.rb