Skip to content

Instantly share code, notes, and snippets.

@carlows
Created January 10, 2019 01:03
Show Gist options
  • Select an option

  • Save carlows/e39db9efa681de7cb1a47262d24a7e29 to your computer and use it in GitHub Desktop.

Select an option

Save carlows/e39db9efa681de7cb1a47262d24a7e29 to your computer and use it in GitHub Desktop.

Revisions

  1. carlows revised this gist Jan 10, 2019. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions weather_struct.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    class AnnualWeather
    Reading = Struct.new(:date, :high, :low)

    def initialize (file_name)
    @readings = []

  2. carlows created this gist Jan 10, 2019.
    21 changes: 21 additions & 0 deletions weather_struct.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    class AnnualWeather
    def initialize (file_name)
    @readings = []

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

    def mean
    return 0.0 if @readings.size.zero?

    total = @readings.reduce(0.0) do |sum, reading|
    sum + (reading.high + reading.low) / 2.0
    end

    total / @readings.size.to_f
    end
    end