Skip to content

Commit

Permalink
specifying access control
Browse files Browse the repository at this point in the history
  • Loading branch information
mblair committed Apr 14, 2011
1 parent 08a620e commit eea7f15
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions ch03/specifying_access_control/08.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Account
attr_accessor :balance
def initialize(balance)
@balance = balance
end
end

class Transaction
def initialize(account_a, account_b)
@account_a = account_a
@account_b = account_b
end
private
def debit(account, amount)
account.balance -= amount
end
def credit(account, amount)
account.balance += amount
end
public
def transfer(amount)
debit(@account_a, amount)
credit(@account_b, amount)
end
end

savings = Account.new(100)
checking = Account.new(200)

puts "Savings balance is #{savings.balance}"
puts "Checking balanace is #{checking.balance}"

trans = Transaction.new(checking, savings)
trans.transfer(50)

puts "Savings balance is #{savings.balance}"
puts "Checking balanace is #{checking.balance}"

0 comments on commit eea7f15

Please sign in to comment.