I am new to Ruby and am currently trying out a few examples from a Ruby book that I use as a guide:
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
def debit(account,amount)
account.balance -= amount
end
def credit(account,amount)
account.balance += amount
end
def transfer(amount)
debit(@account_a, amount)
credit(@account_b, amount)
end
end
savings = Account.new(100)
checking = Account.new(200)
trans = Transaction.new(checking, savings)
trans.transfer(60)
puts savings.balance
puts checking.balance
This is a fairly simple example containing two classes in one script file. I am confused by the type of argument I pass on to lending and debit methods. Based on Java, I still understand types, so obviously, the type of account variable that I am going to, for example, the debit method, must be of type Account.
Since ruby is dynamically typed and does not check the type, how can I safely work with the argument I pass and determine the rest of the method by specifying: account.balance - + amount?
Am I trying to understand what kind of security exists if I pass a link to an object other than an account to the debit method?
, . , , , ... ( , ) , , , ( )? , , , Account.
def credit(account,amount)
account.balance += amount
end
, , ?
, - , . , , java.
.