Created
October 10, 2008 17:34
-
-
Save danielharan/16105 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment