MΓ³dulo 03 Β· Classes & Modules β LecciΓ³n 3 de 6 Β· ~11 min
Instance Methods & self
Behavior lives next to data
A class isn't just a bag of ivars β it's where you put the methods that act on them. price_label computes a display string from @price_per_30_min_cents, the same computed value from Part I's Swift course, and the same one the real backend computes too:
class Walker
attr_accessor :name, :price_per_30_min_cents
def initialize(name, price_per_30_min_cents)
@name = name
@price_per_30_min_cents = price_per_30_min_cents
end
def price_label
"$#{@price_per_30_min_cents / 100} / 30 min"
end
endSay it out loud: this is the exact same idea as Swift's var priceLabel: String { "$\(pricePer30MinCents / 100) / 30 min" } computed property β a value derived from stored data, recalculated fresh on every call, no return needed since it's the method's last expression (implicit return, module 01). And it's also a preview of module 07: once Walker becomes a real ActiveRecord model, this exact method ships unchanged in app/models/walker.rb.