Skip to content

Instantly share code, notes, and snippets.

@sivabudh
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sivabudh/6b8128e4442b26c3fb0a to your computer and use it in GitHub Desktop.
Save sivabudh/6b8128e4442b26c3fb0a to your computer and use it in GitHub Desktop.
This is why I just love Ruby
require 'csv'
module Roster
# Example of each row is: [1,Pac,Sivabudh Umpudh,nil,nil,nil,nil,nil,nil]
def self.parse_from_csv filename
CSV.read(filename)
.drop(1) # remove the first row of CSV which is a header
.map(&:compact) # remove all `nil`
.map { |line_array| [line_array[0], "#{line_array[1]} - #{line_array[2]}"] } # convert ["1", "Pac", "Sivabudh Umpudh"] to ["1", "Pac - Sivabudh Umpudh"]
.to_h # convert array to hash: voila, I now got a hash with key as the ID, and value as my name!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment