Guest User

Untitled

a guest
Feb 20th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. class KeyValuePairs
  2. # Take a file like:
  3. # G6C=24034
  4. # H9E=24049
  5. # and create the hash you'd expect
  6. # if instead you have something like:
  7. # G6C=24034,24036
  8. # H9E=24049
  9. # then use a block to do the specify the value
  10. def self.hash(file_name, separator='=', &block)
  11. contents = IO.readlines(file_name)
  12. contents.inject({}) do |memo,e|
  13. key,val = e.chomp.split(separator)
  14. if block_given?
  15. yield memo, key, val
  16. else
  17. memo[key] = val
  18. end
  19. memo
  20. end
  21. end
  22. end
Add Comment
Please, Sign In to add comment