Skip to content

Commit

Permalink
ruby set and csv
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxGit committed Apr 2, 2016
1 parent 4b67ba9 commit 32c3417
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ruby/EffectiveRuby/set.rb
@@ -0,0 +1,4 @@
s1 = Set.new(["select", "update"])
#=> #<Set: {"select", "update"}>
s1.include?("select")
#=> true
29 changes: 29 additions & 0 deletions ruby/EffectiveRuby/weather.rb
@@ -0,0 +1,29 @@
require 'csv'
require 'set'

class AnnualWeather
attr_reader :readings

Reading = Struct.new(:date, :high, :low) do
def eql?(other)
date.eql?(other.date)
end

def hash
date.hash
end
end

def initialize(filename)
@readings = Set.new

CSV.foreach(filename, headers: true) do |row|
@readings << Reading.new(Date.parse(row[0]),
row[1].to_f,
row[2].to_f)
end
end
end

aw = AnnualWeather.new('weather_data.csv')
p aw.readings
4 changes: 4 additions & 0 deletions ruby/EffectiveRuby/weather_data.csv
@@ -0,0 +1,4 @@
date,high,low
20160401,25,5
20160402,27,8
20160403,30,8

0 comments on commit 32c3417

Please sign in to comment.