title: type of object references in ruby
body: <p>I am new to Ruby and currently trying a few examples from the Ruby book I am using as a guide:</p>
  
  <pre><code>    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
  </code></pre>
  
  <p>This is a pretty straightforward example containing two classes within the same script file. I am confused about the type of the argument I am passing to the credit and debit methods. Coming from Java, I am still thinging in terms of types, so obviously the type of the account variable i am passing to,say the debit method, has to be of type Account.</p>
  
  <p>Since ruby is dynamically typed and is not checking for type, how can I safely operate on the argument I am passing, and define the rest of the method by saying: account.balance -+ amount ?</p>
  
  <p>I am trying to understand, what kind of safety is there if I pass to the debit method a reference to an object other than Account?</p>
  
  <p>When the body of the method below is defined, it uses the given parameter account. Now, I guess I am repeating myself because I still can't grasp the idea... How can I take the argument <em>account</em> (which can be of any type, since no-one is checking) and build some logic by using the dot operator, ask for its instance variable, or call its other methods, and perform calculations on an object which might, or might-not be, the correct kind (or type)? Of course, implicitely, I want it to be of type Account.</p>
  
  <pre><code>def credit(account,amount)
          account.balance += amount
      end
  </code></pre>
  
  <p>Also, how would the same example work if I declared the two classes in different files?</p>
  
  <p>Sincere apologies in advance for the newbie questions, I just find it hard to wrap my mind around the dynamic typing -- or better, no type checking. The book is either a bit vague on this, or I can't shake my tunnel-vision by thinking only in java. </p>
  
  <p>Any practical explanations would be greatly appreciated.</p>