-
-
Save Y2Kot/85e03b0296551e4c02aefa82839d5a73 to your computer and use it in GitHub Desktop.
Seven Languages in Seven Weeks: Ruby, Day 3
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
module ActsAsCsv | |
def self.included(base) | |
base.extend ClassMethods | |
end | |
module ClassMethods | |
def acts_as_csv | |
include InstanceMethods | |
include Enumerable | |
end | |
end | |
module InstanceMethods | |
attr_accessor :headers, :csv_contents | |
def initialize | |
read | |
end | |
def read | |
@csv_contents = [] | |
filename = self.class.to_s.downcase + '.csv' | |
file = File.new(filename) | |
@headers = parse_row file.gets | |
file.each do |row| | |
@csv_contents << CsvRow.new(@headers, parse_row(row)) | |
end | |
end | |
def parse_row(row) | |
row.chomp.split(', ') | |
end | |
def each | |
@csv_contents.each { |row| yield row } | |
end | |
class CsvRow | |
def initialize(headers, row) | |
@headers = headers | |
@row = row | |
end | |
def respond_to?(sym) | |
@headers.index(name.to_s) || super(sym) | |
end | |
def method_missing name, *args, &block | |
index = @headers.index(name.to_s) | |
if index | |
@row[index] | |
else | |
super | |
end | |
end | |
end | |
end | |
end | |
class RubyCsv | |
include ActsAsCsv | |
acts_as_csv | |
end | |
csv = RubyCsv.new | |
puts csv.headers.inspect | |
puts csv.csv_contents.inspect | |
csv.each { |row| puts "#{row.name}, #{row.age}" } |
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
$ ruby csv_new.rb | |
["name", "location", "age"] | |
[#<ActsAsCsv::InstanceMethods::CsvRow:0x25814 @row=["Jim", "Menlo Park", "27"], @headers=["name", "location", "age"]>, #<ActsAsCsv::InstanceMethods::CsvRow:0x25738 @row=["Bob", "Palo Alto", "37"], @headers=["name", "location", "age"]>, #<ActsAsCsv::InstanceMethods::CsvRow:0x2565c @row=["Steve", "NYC", "28"], @headers=["name", "location", "age"]>] | |
Jim, 27 | |
Bob, 37 | |
Steve, 28 |
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
module ActsAsCsv | |
def self.included(base) | |
base.extend ClassMethods | |
end | |
module ClassMethods | |
def acts_as_csv | |
include InstanceMethods | |
end | |
end | |
module InstanceMethods | |
attr_accessor :headers, :csv_contents | |
def initialize | |
read | |
end | |
def read | |
@csv_contents = [] | |
filename = self.class.to_s.downcase + '.csv' | |
file = File.new(filename) | |
@headers = file.gets.chomp.split(', ') | |
file.each do |row| | |
@csv_contents << row.chomp.split(', ') | |
end | |
end | |
end | |
end | |
class RubyCsv | |
include ActsAsCsv | |
acts_as_csv | |
end | |
csv = RubyCsv.new | |
puts csv.headers.inspect | |
puts csv.csv_contents.inspect |
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
$ ruby csv_original.rb | |
["name", "location", "age"] | |
[["Jim", "Menlo Park", "27"], ["Bob", "Palo Alto", "37"], ["Steve", "NYC", "28"]] |
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 LinkedInProfile | |
SIMPLE_PROFILE_FIELDS = %w[id summary headline honors interests specialties industry first_name last_name public_profile_url picture_url associations] | |
SIMPLE_PROFILE_FIELDS.each do |field| | |
define_method(field.to_sym) do | |
@json[field] | |
end | |
end | |
def initialize(json) | |
@json = json | |
end | |
end |
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
name | location | age | |
---|---|---|---|
Jim | Menlo Park | 27 | |
Bob | Palo Alto | 37 | |
Steve | NYC | 28 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment