Skip to content

Instantly share code, notes, and snippets.

@danielharan
Created October 10, 2008 17:34
Show Gist options
  • Save danielharan/16105 to your computer and use it in GitHub Desktop.
Save danielharan/16105 to your computer and use it in GitHub Desktop.
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