ruby:类实例变量与实例变量 [英] ruby: class instance variables vs instance variables

查看:143
本文介绍了ruby:类实例变量与实例变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的想法是为来自java背景的人们创建一个社区Wiki,因为阅读了大量的解释,直到我实际尝试了几件事并且困惑的各个部分开始,我才明白了什么.找到他们的地方.但是我首先需要确保我做对了.出于这样的背景,让我感到困惑的是@variable可能意味着2个非常不同的东西. 这是一个示例:

my idea is to create a community wiki for people that come from a java background because reading a lot of explanations, I couldn't comprehend anything until I actually tried a couple of things and the pieces of the puzzle started to find their places. But I first need to make sure I'm getting it right. Coming from such background it was very confusing for me to find out that @variable may mean 2 very different things. Here is an example:

class Test
  @ins = "gah"
  def self.ins
    puts @ins
  end

  def initialize()
    @ins = "wtf?"
  end
  def ins2
    puts @ins
  end
end

据我了解,第一个@ins是表示类Test的对象的实例变量.第二个@ins是类Test的对象中的实例变量.

As far as I understand, the first @ins is an instance variable of the object representing the class Test. The second @ins is an instance variable in an object of class Test.

现在,事情开始对我有意义.这里有几个例子:

Now things start to make some sense to me. Here a couple of examples:

[14] pry(main)> test.ins2
wtf?

我们正在调用对象的方法,它返回对象的实例变量.

We are calling a method of an object and it returns the object's instance variable.

[15] pry(main)> test.ins
NoMethodError: undefined method `ins' for #<Test:0x000000017d9348 @ins="wtf?">

我们正在尝试通过对象调用类方法,该方法属于该类,因此我们正在获取NoMethodError

We are trying to call a class method through an object, this method is of the class so we are getting NoMethodError

[16] pry(main)> Test.ins
gah

我们正在调用类方法,以便它可以正确看到类对象的实例变量.

We are calling a class method so it properly sees the instance variable of the class object.

[17] pry(main)> Test.ins2
NoMethodError: undefined method `ins2' for Test:Class

我们正在通过类调用对象方法,这是不正确的,因此抛出NoMethodError.

We are calling an object method through the class which is incorrect so throwing NoMethodError.

以上所有操作都是使用ruby 2.0进行的.那我在问什么呢?

All of the above was performed with ruby 2.0. So what am I asking?

  • 我说得对吗?
  • 我是否正确理解了红宝石术语?
  • 在设计合理的应用中,对有意义的类实例变量的任何实际用法?还是这些仅仅是更好的@@ class变量?
  • Am I getting it right?
  • Am I getting the ruby terminology correct?
  • Any real usage of class instance variables that make sense in a properly designed app? Or are these simply the better @@class variables?

推荐答案

发现@variable可能意味着2个非常不同的东西,这让我感到困惑.

it was very confusing for me to find out that @variable may mean 2 very different things.

不,不是.类是对象,就像其他任何对象一样.它们可以像其他任何对象一样具有实例变量.他们可以像其他任何对象一样具有实例方法.实际上,与Java中具有三种不同的方法"(实例方法,静态方法和构造函数)的Java不同,在Ruby中,仅存在一种方法:实例方法.

No, it doesn't. Classes are objects just like any other object. They can have instance variables just like any other object. They can have instance methods just like any other object. In fact, unlike Java, which has three different kinds of "methods" (instance methods, static methods and constructors), in Ruby, there is exactly one kind of method: instance methods.

将类作为对象的美妙之处在于,@variable始终表示完全相同的事物.

The beauty of having classes being objects is precisely that @variable always means exactly the same thing.

没有像类实例变量这样的东西:它只是一个普通的实例变量,与其他变量一样.该对象恰巧是Class的实例,但这不会改变实例变量的性质.类String的对象的实例变量不是字符串实例变量,而只是一个实例变量.同样,类Class的对象的实例变量只是一个实例变量.

There is no such thing as a class instance variable: it's just a normal instance variable like any other. The object happens to be an instance of Class, but that doesn't change the nature of the instance variable. An instance variable of an object of class String is not a string instance variable, it's just an instance variable. Likewise, an instance variable of an object of class Class is just an instance variable.

没有这样的类方法:它只是对象的普通单例方法,恰好是Class的实例. (从根本上说,也没有诸如singleton方法之类的东西:它只是对象的singleton类的普通实例方法.)

There is no such thing as a class method: it's just a normal singleton method of an object which happens to be an instance of Class. (Acually, there's no such thing as a singleton method either: it's just a normal instance method of the object's singleton class.)

注意:Rubyists可以在随意交谈中使用术语类方法".但这并不意味着实际上存在类方法,而仅意味着类对象的单例类的实例方法"是一口大话.重要的是:因为类是对象,所以它们像所有其他对象一样精确地工作.它们可以具有实例方法(在类Class中定义或从ModuleObjectKernelBasicObject继承),它们可以具有单例方法"(实际上是其各自单例类的实例方法) ),它们可以具有实例变量.

Note: Rubyists may use the term "class method" in casual conversation. But that doesn't mean that class methods actually exist, it only means that "instance method of the class object's singleton class" is a mouthful. The important thing is: because classes are objects, they work exactly like all other objects. They can have instance methods (defined in class Class or inherited from Module, Object, Kernel or BasicObject), they can have "singleton methods" (which really are instance methods of their respective singleton classes), they can have instance variables.

它们还可以 具有类变量(@@variables)…太奇怪了.忽略它们:-)

They can also have class variables (@@variables) … those are weird. Ignore them :-)

这篇关于ruby:类实例变量与实例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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