Skip to content

Instantly share code, notes, and snippets.

@fourseven
Created June 27, 2013 01:44

Revisions

  1. Mathew Hartley created this gist Jun 27, 2013.
    36 changes: 36 additions & 0 deletions classes.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    class MailerContact
    def initialize(first_name, last_name)
    @first_name, @last_name = first_name, last_name
    end

    def full_name
    "#{@first_name} #{@last_name}"
    end
    end

    class Duration
    def initialize(starting_at, ending_at, unit)
    @starting_at, @ending_at, @unit = starting_at, ending_at, unit
    end

    def calculate
    return 0 unless @ending_at and @starting_at
    if @unit == 'hour'
    ((@ending_at - @starting_at) / 1.hour).round(2)
    else # second
    @ending_at - @starting_at
    end
    end
    end

    class Price
    def initialize(rate, duration)
    @rate, @duration = rate, duration
    end

    def calculate
    @rate * @duration
    end
    end


    22 changes: 22 additions & 0 deletions structs.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    class MailerContact < Struct.new(:first_name, :last_name)
    def full_name
    "#{first_name} #{last_name}"
    end
    end

    class Duration < Struct.new(:starting_at, :ending_at, :unit)
    def calculate
    return 0 unless ending_at and starting_at
    if unit == 'hour'
    ((ending_at - starting_at) / 1.hour).round(2)
    else # second
    (ending_at - starting_at).round(0)
    end
    end
    end

    class Price < Struct.new(:rate, :duration)
    def calculate
    rate * duration
    end
    end