Skip to content

Instantly share code, notes, and snippets.

@ssrihari
Last active December 15, 2015 02:19
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ssrihari/5185977 to your computer and use it in GitHub Desktop.
Save ssrihari/5185977 to your computer and use it in GitHub Desktop.
dog.legs.walk! if dog.normal?
dog.hover_craft.hover! if dog.robot?
class Dog
def move!
legs.walk!
end
end
dog.move!
def sales_tax
return 0 if purchaser.exempt_from_taxes?
price * .10
end
def car.brake
switch_on_brake_light
unless stopped?
slow_down
end
end
def self.import_from_csv(csv)
csv.each_line do |line|
if line["type"] == "distributer"
Document.new(line["foo"], line["retail_price"])
else
Document.new(line["foo"], line["wholesale_price"])
end
end
end
def self.import_line(line)
if line["type"] == "distributer"
Document.new(line["foo"], line["retail_price"])
else
Document.new(line["foo"], line["wholesale_price"])
end
end
def self.import_from_csv(csv)
csv.each_line do |line|
import_line(line)
end
end
def expiry_date
case plan_type
when 'subscription' then next_month
when 'prepaid' then never
end
end
def monthly_charge
case plan_type
when 'subscription' then 300
when 'prepaid' then 0
end
end
def charge_per_rental
case plan_type
when 'subscription' then 0
when 'prepaid' then 100
end
end
class SubscriptionPlan
attributes :expiration_date => never, :monthly_charge => 300, :charge_per_rental => 0
end
class PrepaidPlan
attributes :expiration_date => never, :monthly_charge => 300, :charge_per_rental => 0
end
def plan_type
case plan_type
when 'subscription' then SubscriptionPlan.new
when 'prepaid' then PrepaidPlan.new
end
end
def expiry_date
plan_type.expiry_date
end
def monthly_charge
plan_type.monthly_charge
end
def charge_per_rental
plan_type.charge_per_rental
end
if account.balance >= amount
account.deduct(amount)
else
raise NotEnoughBalance
end
class Account
def deduct(amount)
balance -= amount
end
end
account.deduct(amount)
class Account
def deduct(amount)
raise NotEnoughBalance unless balance >= amount
balance -= amount
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment