Ruby/Rails:了解Ruby的getter-setter方法和实例 [英] Ruby/Rails: Understanding ruby getter-setter methods and instances

查看:74
本文介绍了Ruby/Rails:了解Ruby的getter-setter方法和实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一般对红宝石和编程都不熟悉,并试图掌握一些关键概念. 给定我有一个狗类,具有以下特征.

I am new to ruby and programming in general and am trying to grasp a few key concepts. Given I have a class Dog, with the below characteristics.

class Dog
  attr_accessor :type, :popularity, :total

  def initialize(type = nil)
    @type = type
  end

  def total_dogs
    Dog.count
  end

  def total
    Dog.where(:type => self.type).size
  end

  def popularity
    total.to_f/total_dogs
  end
end

我想了解的是,ruby如何通过getter/setter方法将属性持久化为实例.对我来说很清楚,如果我实例化一个新实例,然后将属性保存到该实例,则这些属性将绑定到该实例,因为如果我查看对象,这些属性将如下显示:

What I am trying to understand, is how ruby persists attributes to an instance via getter/setter methods. Its clear to me that if I instantiate a new instance and then save attributes to that instance, those attributes are tied to that instance because if I look at the object the attributes appear as such:

 @dog = Dog.new
 @dog
 => #<Dog:0x007fa8689ea238 @type=nil> 

我很容易理解,当我在@dog对象周围传递@dog对象时,其@type属性始终为nil. 但是,我很难理解的情况是我是否将此@dog对象传递给另一个类.就像我做了一样:

Its easy for me to understand that as I pass the @dog object around its always going to have the @type attribute as nil. However, the situation I am having trouble understanding is if I pass this @dog object to another class. Like if I did:

 Owner.new(@dog)

当我进入所有者类并调用@ dog.popularity时,它如何知道该实例的人气值?在运行时是否处理了所有方法,然后该实例始终总是与当时的值绑定在一起?道歉,如果这没有任何意义,或者我离我们很遥远.

When I am in the owner class and I call @dog.popularity how does it know the value of popularity for that instance? At runtime are all methods processed and then that instance just always is tied to the value at the time? Apologies if this makes no sense or I am way off.

推荐答案

执行时

@dog = Dog.new

你做两件刺耳的事

1)为代码当前位于其中的任何对象创建一个实例变量@dog

1) Create an instance variable @dog for whatever object your code is currently inside

2)实例化Dog的新实例(及其所有方法和属性),并将其引用分配给@dog

2) Instantiate a new instance of Dog (with all its methods and attributes) and assign a reference to it to @dog

@dog是一个变量,它恰好指向您在该点创建的Dog实例(类的实例"与对象"的含义通常相同).您可以将其他变量设置为指向同一实例,在Ruby中,这通常是传递数据的方式.对象包含实例变量,并且这些实例变量指向更多对象.

@dog is a variable, that just happens to point at the Dog instance ("instance of class" generally same meaning as "object") you created at that point. You can set other variables to point to the same instance, and in Ruby this is generally how you pass data around. Objects contain instance variables, and those instance variables point to yet more objects.

使用赋值运算符(即"="),您可以将变量指向其他任何对象.

Using the assignment operator (i.e "=") you can point a variable at any other object.

依次回答您的问题:

当我在所有者类中并调用@ dog.popularity时,它如何 知道该实例的受欢迎程度的价值吗?

When I am in the owner class and I call @dog.popularity how does it know the value of popularity for that instance?

在Ruby(通常是OO语言)中,您必须小心,以区分描述和问题中的类和对象. Ruby我假设您是在Owner类中引用一行代码,并且您打算将其与owner对象一起使用.我还要假设@dog是您已添加到所有者的属性.

You have to be careful in Ruby (and OO languages in general) to differentiate between class and object in your descriptions and questions. Ruby I'm assuming you are referring to a line of code in the Owner class, and that you intend it to work with an owner object. I'd also assume that @dog is an attribute you have added to Owner.

在这种情况下,Ruby知道是因为@dog指向您添加到所有者的Dog对象.每个Dog对象都有其自己的所有Dog实例变量的副本.不过,您确实需要在Ruby中注意,因为变量 point 指向对象,您并不会简单地将同一个Dog对象传递给所有所有者(即,它们都有效地共享一条狗).因此,您需要了解何时创建新实例(通过新实例)以及何时仅处理现有引用.

In which case, Ruby knows because @dog points to the Dog object that you added to owner. Each Dog object has its own copy of all of Dog's instance variables. You do need to take care in Ruby though, because variables point to objects, that you aren't simply passing in the same Dog object to all the owners (i.e. they all effectively share a single dog). So you need to understand when you are creating new instances (via new) and when you are simply handling existing references.

在运行时,将处理所有方法,然后仅处理该实例 总是和当时的价值联系在一起吗?

At runtime are all methods processed and then that instance just always is tied to the value at the time?

不.在运行时,基本Ruby将仅执行您已编码的分配.实例变量甚至可能不存在,直到分配它们的代码已运行.如果您使用attr_reader等方法,则变量将至少存在(但将为nil,除非您在初始化期间分配了某些内容)

No. At runtime, basic Ruby will only perform the assignments that you have coded. Instance variables may not even exist until the code that assigns them has been run. If you use attr_reader etc methods, then the variables will at least exist (but will be nil unless you assign something during initialize)

这篇关于Ruby/Rails:了解Ruby的getter-setter方法和实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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