红宝石中对象引用的类型 [英] type of object references in ruby

查看:114
本文介绍了红宝石中对象引用的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Ruby的新手,目前正在尝试使用我作为指南的Ruby书中的一些示例:

I am new to Ruby and currently trying a few examples from the Ruby book I am using 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

这是一个非常简单的示例,在同一脚本文件中包含两个类.我对传递给贷方和借方方法的参数类型感到困惑.来自Java,我仍然在类型方面有所顾忌,因此显然,要传递给我的帐户变量的类型(例如,借方方法)必须是Account类型.

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.

由于ruby是动态类型的,并且不检查类型,因此我如何安全地对传递的参数进行运算,并通过说出account.balance-+ amount来定义方法的其余部分?

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 ?

我试图理解,如果将借记帐户以外的其他对象的引用传递给借记方法,那会有什么安全性?

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?

定义以下方法的主体时,它将使用给定的参数帐户.现在,我想我正在重复自己的步骤,因为我仍然无法理解这个主意...我该如何接受 account 参数(可以是任何类型,因为没有人在检查),并且通过使用点运算符构建一些逻辑,询问其实例变量,或调用其其他方法,并对可能是或不是正确类型(或类型)的对象执行计算?当然,隐式地,我希望它的类型为Account.

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 account (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.

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

此外,如果我在不同文件中声明两个类,那么同一示例将如何工作?

Also, how would the same example work if I declared the two classes in different files?

由于对新手问题事先表示歉意,所以我发现很难全神贯注于动态类型-更好的是,不进行类型检查.这本书要么对此含糊不清,要么仅靠Java就无法撼动我的隧道视野.

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.

任何实际的解释将不胜感激.

Any practical explanations would be greatly appreciated.

推荐答案

您在Ruby中没有任何类型安全性.对Ruby而言,最重要的是对象是否可以响应其接收的消息.您可以传递完全荒唐的东西,而Ruby并不会阻止您.但是,如果传入的对象不响应所发送的消息(在本例中为+和-),则在代码尝试发送无效消息时会出现NoMethodError.

You don't have any type safety in Ruby. All that matters to Ruby is whether an object can respond to the messages it's receiving. You can pass in something completely nonsensical and Ruby won't do anything to stop you at that point. But if you pass in an object that doesn't respond to the messages you're sending (+ and - in this case), you'll get a NoMethodError when your code tries to send the invalid message.

通常,解决此问题的方法是:不要传递错误的类型.它知道这听起来很微弱,但这几乎是您需要做的-确保您通过的是正确的事情.为程序编写测试,以确保您正在按照自己的意愿去做. Ruby在单元测试方面非常重要.如果您真的担心参数是正确的事情,则可以显式检查其类(raise 'WTF?' unless object.class == String),也可以尝试将其转换为正确的类(通过定义to_foo类型的方法)

In general, the solution to this is: Don't pass in the wrong type. It know it sounds weak, but that's pretty much what you need to do — ensure you're passing the right thing. Write tests for your programs to make sure you're doing what you mean to do. Ruby is very big on unit-testing. If you're really worried about an argument being the correct kind of thing, you can either explicitly check its class (raise 'WTF?' unless object.class == String) or you can try to convert it to the right class (by defining a to_foo-type method).

作为回报,您从很大程度上不必关心类型.只要对象响应您发送的消息,传递的内容都没有关系.这使得诸如模拟和代理之类的事情变得非常简单.

In return for this, you're largely freed from caring about types. As long as the object responds to the messages you send, it doesn't matter what you pass in. This makes things like mocks and proxies really easy.

这篇关于红宝石中对象引用的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆