Joel VanderWerf
3/18/2009 4:55:00 AM
Jason Lillywhite wrote:
> Is there a way to get local state variables to be unique for different
> objects? See my example bank account:
>
> class Acc
> @@balance = 100
> def withdraw( amount )
> if @@balance >= amount
> @@balance = @@balance - amount
> else
> "Insufficient Funds"
> end
> end
> @@balance
> end
@@balance is a _class_ variable, shared by all instances of the class
(and potentially subclasses), you probably want an instance variable:
class Acc
def initialize bal
@balance = bal
end
def withdraw( amount )
if @balance >= amount
@balance = @balance - amount
else
"Insufficient Funds"
end
end
end
acc = Acc.new 100
puts acc.withdraw(12)
puts acc.withdraw(120)
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407