danielharan (owner)

Revisions

gist: 16105 Download_button fork
public
Public Clone URL: git://gist.github.com/16105.git
Embed All Files: show embed
key_value_pairs.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class KeyValuePairs
  # Take a file like:
  # G6C=24034
  # H9E=24049
  # and create the hash you'd expect
  # if instead you have something like:
  # G6C=24034,24036
  # H9E=24049
  # then use a block to do the specify the value
  def self.hash(file_name, separator='=', &block)
    contents = IO.readlines(file_name)
    contents.inject({}) do |memo,e|
      key,val = e.chomp.split(separator)
      if block_given?
        yield memo, key, val
      else
        memo[key] = val
      end
      memo
    end
  end
end