Last active
August 29, 2015 14:17
-
-
Save sivabudh/6b8128e4442b26c3fb0a to your computer and use it in GitHub Desktop.
This is why I just love Ruby
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
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