Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Account
- # constructor used when Account.new(...) called
- def initialize(starting_balance=0) # optional argument
- @balance = starting_balance
- end
- def balance # instance method
- @balance # instance var: visible only to this object
- end
- def balance=(new_amount) # note method name: like setter
- @balance = new_amount
- end
- def deposit(amount)
- @balance += amount
- end
- @@bank_name = "MyBank.com" # class (static) variable
- # A class method
- def self.bank_name # note difference in method def
- @@bank_name
- end
- # or: def SavingsAccount.bank_name ; @@bank_name ; end
- end
- armando = Account.new(1000)
- dave = Account.new(5000)
- billg = Account.new(1e9)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement