简体   繁体   English

为什么在Ruby类模型中使用@变量?

[英]Why are @ variables used in Ruby class models?

Why are @variables needed in classes? 为什么类中需要@variables What value do they add? 他们增加了什么价值? I couldn't find anything online for this but maybe I'm searching for the wrong terms. 我在网上找不到任何东西,但也许我正在寻找错误的条款。 Is there a resource online I can look this up? 有在线资源我可以查看吗? Thanks! 谢谢!

car.rb car.rb

class Car
  attr_accessor :make, :model

  def initialize(make = '')
    @make = ''
    @model = ''
  end
end

These variables are called instance variables. 这些变量称为实例变量。 Every instance of the class has it's own copy of these variables. 该类的每个实例都有自己的这些变量的副本。

In your example, you would like every instance of the class Car to have it's own make and model. 在您的示例中,您希望Car类的每个实例都拥有自己的品牌和型号。

Note the following example 请注意以下示例

car1 = Car.new("Toyota", "Carola")
car2 = Car.new("Mitsubishi", "Lancer")

Both car1 and car2 each have their own private make and model . car1和car2都有自己的私人makemodel The way to tell the Ruby interpreter to do this is to use @. 告诉Ruby解释器执行此操作的方法是使用@。

这是在ruby中定义实例变量的语法。

Instance variables are available in all areas of each instance of a class. 实例变量可用于每个类实例的所有区域。

Each time you create an instance of car the variable is specific to that particular one. 每次创建汽车实例时,变量都特定于该特定的变量。

eg 例如

car1 = Car.new('Ford', 'Falcon')
car2 = Car.new('Toyota', 'Camry')

Now car1 and car2 have different instances of @make and @model. 现在car1和car2有不同的@make和@model实例。

If you declare the variable as a class variable using @@make , then every Car has access to it and every time it is changed, it is changed for everyone. 如果您使用@@make将变量声明为类变量,那么每个Car都可以访问它,并且每次更改它时,它都会针对每个人进行更改。

Basically class variables allow you to put some 'walls' around your data. 基本上类变量允许您在数据周围放置一些“墙”。

The attr_accessor creates two methods in your call attr_accessor在您的调用中创建两个方法

def make=(value)
@make = value
end

def make
@make
end

This allows you to call the instance variable within and from outside your class without the @ 这允许您在没有@的情况下在类外部调用实例变量。

eg 例如

car1.make

returns 回报

'Ford'

www.codecademy.com has some great free courses in basic Ruby that will teach you this stuff really well. www.codecademy.com有一些很棒的免费基础Ruby课程,可以很好地教你这些东西。

Objects are said to have behaviours and properties, much like real world object. 据说对象具有行为和属性,就像现实世界对象一样。 The properties of objects are defined by instance variables. 对象的属性由实例变量定义。 And @ prefix is ruby's way of telling particular variable is an instance variable/characteristic. 而@前缀是ruby告诉特定变量的方式是实例变量/特征。

What value they add ? 他们添加了什么价值?

They help you to have a variable that is accessible across all your instance methods. 它们可以帮助您获得可在所有实例方法中访问的变量。

Let us disect your example 让我们来看看你的例子吧

   class Car
    attr_accessor :make, :model #attr_accessor methods creates a getter method.    car_instance.make and
                 #a setter method car_instance.make=(val), which otherwise you would have to do explicitly. 

    def initialize(make = '')   #constructor
      @make = '' #initializing your instance variables. I guess you meant @make = make here     
      @model = ''
    end
   end

I couldn't find anything online for this but maybe I'm searching for the wrong terms. 我在网上找不到任何东西,但也许我正在寻找错误的条款。

Yup you were. 是的,你是谁。

Is there a resource online I can look this up? 有在线资源我可以查看吗?

Yup, Ruby User Guide By Matz 是的, Ruby用户指南由Matz

Bit more details 更详细一点

In ruby, the scope of a variable inside a class is identified by the compiler by first two characters of the varaible name. 在ruby中,类中变量的范围由编译器通过可变名称的前两个字符标识。

  • @@variable_name -> Two '@' s. @@ variable_name - >两个'@'。 Class variables/ Scope across all instances of the class. 类的所有实例中的类变量/范围。 Eg: If you want to have a count on total number of cars you made. 例如:如果您想计算您制造的汽车总数。

  • @variable -> One '@' Instance varaibles/ Scope across an instance. @variable - >一个实例的一个'@'实例变量/范围。

  • variable -> No '@' Local variables/ Scope to the block they belong. 变量 - >否'@'局部变量/它们所属的块的范围。

Look at the below code,which I changed a bit for understanding: 看下面的代码,为了理解我改变了一下:

class Car
  attr_accessor :make, :model

  def initialize(make = '')
    @make = make
    @model = ''
  end
end

car1 = Car.new("Toyota")
car2 = Car.new("Roxin")
p car1.instance_variables #<~~ A
p car2.instance_variables #<~~ B
p car1 #<~~C
p car2#<~~D

Output: 输出:

[:@make, :@model]
[:@make, :@model]
#<Car:0x1bc89e8 @make="Toyota", @model="">
#<Car:0x1bc8940 @make="Roxin", @model="">

Explanation: Here in A and B you might think of that both car1,car2 are using the same copy of instance variables. 说明:AB中,您可能会想到car1,car2都使用相同的实例变量副本。 But Not is the case. 但事实并非如此。 See when I am printing car1 and car2, in C and D ,outputs shows that car1 and car2 are holding different values for the instance variable @make . 看我何时打印car1和car2,在CD中 ,输出显示car1和car2为实例变量@make保持不同的值。 This fact objects has their own unique instance variables. 这个事实对象有自己独特的实例变量。

Hope this helps. 希望这可以帮助。

Cheers!! 干杯!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM