@ericturnerdev
github.com/etdev
by Eric Turner
@sandimetz
def price(weight, discount = nil)
if weight <= 10
return_price = 1
elsif weight <= 20
return_price = 2
else weight <= 30
return_price = 3
end
if discount
return_price
else
return_price - discount
end
end
# => price(5, 0.2) = 0.8
# => price(25) = 3
def price(weight, discount = nil)
return_price = weight/10 + 1
if discount
return_price
else
return_price - discount
end
end
# => price(5, 0.2) = 0.8
# => price(25) = 3
def price(weight, discount = nil)
return_price = weight/10 + 1
discount ? return_price - discount : return_price
end
# => price(5, 0.2) = 0.8
# => price(25) = 3
def price(weight, discount = nil)
(weight/10 + 1) - (discount || 0)
end
# => price(5, 0.2) = 0.8
# => price(25) = 3
def price(weight, color, length, witdh, height, discount = nil)
...
end
def price(weight, color, length, options = {})
width = options[:width]
height = options[:height]
discount = options[:discount]
...
end
class Car
attr_accessor :color, :weight, :length, :width, :height
...
end
def price(car)
...
end
class Dashboard
def initialize(user)
@user = user
end
def new_status
@new_status ||= Status.new
end
def statuses
Status.for(user)
end
def notifications
@notifications ||= user.notifications
end
private
attr_reader :user
end
class DashboardsController < ApplicationController
before_filter :authorize
def show
@dashboard = Dashboard.new(current_user)
end
end
<%= render 'profile' %>
<%= render 'notifications', notifications: @dashboard.notifications %>
<%= render 'statuses/form', status: @dashboard.new_status %>
<%= render 'statuses', statuses: @dashboard.statuses %>