Advertisement
saasbook

bank account example

Jan 22nd, 2012
4,090
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.71 KB | None | 0 0
  1. class Account
  2.   # constructor used when Account.new(...) called
  3.   def initialize(starting_balance=0) # optional argument
  4.     @balance = starting_balance
  5.   end
  6.   def balance    # instance method
  7.     @balance   # instance var: visible only to this object
  8.   end
  9.   def balance=(new_amount)  # note method name: like setter
  10.     @balance = new_amount
  11.   end
  12.   def deposit(amount)
  13.     @balance += amount
  14.   end
  15.   @@bank_name = "MyBank.com"    # class (static) variable  
  16.   # A class method
  17.   def self.bank_name   # note difference in method def
  18.     @@bank_name
  19.   end
  20.   # or: def SavingsAccount.bank_name ; @@bank_name ; end
  21. end
  22.  
  23. armando = Account.new(1000)
  24. dave = Account.new(5000)
  25. billg = Account.new(1e9)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement