Java – object reference types in Ruby

I am a novice in ruby and am currently trying to use some examples in the ruby book 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 very simple example, including two classes in the same script file I'm confused about the type of argument passed to credit and debit card methods From Java, I'm still in type, so it's obvious that the type I pass to the account variable, such as debit method, must be of account type

Since ruby is dynamically typed and does not check the type, how can I safely manipulate the parameters I pass by saying: account Balance – amount to define the rest of the method?

I'm trying to understand what security would be if I passed a reference to an object other than the account to the debit method?

When defining the body of the following method, it uses the given parameter account Now, I think I'm repeating myself because I still can't understand this idea... How can I take the parameter account (which can be of any type because no one checks) and use points to build some logical operators, request their instance variables, or call other methods, and perform calculations on objects that may or may not be of the correct type (or type)? Of course, implicitly, I want it to be of type account

def credit(account,amount)
        account.balance += amount
    end

In addition, if I declare these two classes in different files, how will the same example work?

Sincerely apologizing for novice problems, I find it difficult to type around dynamics - or better, without type checking This book is either a little vague, or I can only think in Java and can't shake my tunnel vision

Any practical explanation would be appreciated

Solution

There is no type security in ruby For ruby, the most important thing is whether the object can respond to the message it receives You can pass something completely meaningless, and Ruby won't do anything to stop you However, if the incoming object does not respond to the message you are sending (in this case), you will receive nomethoderror when the code attempts to send an invalid message

Generally speaking, the solution is: do not pass in the wrong type It knows it sounds weak, but that's almost what you need to do - make sure you pass the right thing Write tests for your program to make sure you're doing what you want Ruby is very important in unit testing If you are really worried that a parameter is the right thing, you can explicitly check its class (throw 'WTF?' unless object. Class = = string) or you can try to convert it to the right class (by defining a method of type to_foo)

In return, you basically don't care about type As long as the object responds to the message you send, it doesn't matter what you enter This makes things like simulations and proxies very simple

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>