Skip to content

Instantly share code, notes, and snippets.

@fourseven
Created June 27, 2013 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fourseven/5873350 to your computer and use it in GitHub Desktop.
Save fourseven/5873350 to your computer and use it in GitHub Desktop.
Notes/examples for better code
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
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