Created
June 27, 2013 01:44
-
-
Save fourseven/5873350 to your computer and use it in GitHub Desktop.
Notes/examples for better code
This file contains hidden or 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 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 | |
This file contains hidden or 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 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment