“如果/除非"的类别是什么?等等 [英] What is the class of "if/unless" etc
问题描述
考虑到Ruby中的所有内容都是对象,我们可以打开irb并键入4.class
和"Text".class
之类的东西来查看对象来自哪个类,为什么if.class
和unless.class
没有给出返回值?
Considering that everything in Ruby is an object and we can open irb and type things like 4.class
and "Text".class
to see from which class an object is, why do if.class
and unless.class
give no return value?
推荐答案
考虑到Ruby中的所有内容都是对象
Considering that everything in Ruby is an object
这取决于您对"对象"和每一个"事物"的定义. " Object "可以表示"可由程序操纵的实体"(从现在开始,我将其称为 object ),或"是对象系统成员的值"(从现在开始,我将其称为Object
).
That depends on your definition of "object" and every-"thing". "Object" can mean "entity that can be manipulated by the program" (which I will call object from now on), or "value that is a member of the object system" (which I will call Object
from now on).
在Ruby中,程序可以操纵的所有内容(即每个对象)也是Object
,即类的实例.例如,这与Java不同,在Java中,原始语可以由程序操纵(即,在这个词的意义上是 objects ),但不是Objects
.在Ruby中,不存在这种区别:每个对象是一个Object
,每个Object
也是一个对象.
In Ruby, everything that can be manipulated by the program (i.e. every object) is also an Object
, i.e. an instance of a class. This is unlike Java, for example, where primitives can be manipulated by the program (i.e. are objects in that sense of the word), but aren't Objects
. In Ruby, this distinction doesn't exist: every object is an Object
and every Object
is also an object.
但是,该语言中有事物,这些 不能被程序操纵,也不是类的实例,即它们都不是对象或Object
.例如,这些是方法,变量,语法,参数列表,参数列表,关键字.
However, there are things in the language, which cannot be manipulated by the program and which aren't instances of a class, i.e. they are neither object s nor Object
s. These are, for example, methods, variables, syntax, parameter lists, argument lists, keywords.
注意:您可以使用Ruby的反射API为您提供一个表示方法或参数列表的对象,但是该对象只是一个代理,不是真实的东西.
Note: you can use Ruby's reflection API to give you an object that represents a method or a parameter list, but that object is only a proxy, it is not the real thing.
所以,当我们说"一切都是对象"时,我们真正的意思是每个对象都是Object
",即可以被程序操纵的对象也是对象系统的成员,换句话说,对象系统之外没有值(与Java中的原语不同). 不是并不意味着该语言中存在的所有内容都可以在运行时由程序进行操纵.
So, when we say "everything is an object", what we really mean is that "every object is an Object
", i.e. that everything which can be manipulated by the program is also a member of the object system, or in other words, there are no values outside of the object system (unlike primitives in Java). We do not mean that everything that exists in the language can also be manipulated at runtime by the program.
为什么
if.class
和unless.class
没有给出返回值
why do
if.class
andunless.class
give no return value
首先,即使如果 if
是一个对象,那些对象也不会按照您的想法去做:当您说诸如foo
在Ruby中,这意味着取消引用局部变量foo
"或以self
作为隐式接收器且没有参数列表的情况下调用方法foo
".因此,
Well, first off, even if if
were an object, those don't do what you think they do: when you say something like foo
in Ruby, it means either "dereference the local variable foo
" or "call the method foo
with self
as the implicit receiver and no argument list". So,
if.class
可以为您提供由局部变量if
引用的对象的的类,还是由方法if
提供的对象返回的的类,但是从不 if
本身的类.
would either give you the class of the object referenced by the local variable if
or the class of the object returned by the method if
, but never the class of if
itself.
但是if
控制流关键字不是对象(无论是 object 还是Object
),因为关键字和控制流在Ruby中不是对象.
But the if
control flow keyword isn't an object, anyway (neither an object nor an Object
) because keywords and control flow aren't objects in Ruby.
在Matz和David Flanagan的 The Ruby Programming Language 中它在第2页上说:
In the book The Ruby Programming Language by Matz and David Flanagan it says on page 2:
每个值都是一个对象
every value is an object
请注意,它不是说所有-事情,而是说每个值.
Note, it doesn't say every-thing, only every value.
另请参阅问题变量是红宝石中的对象吗?
这篇关于“如果/除非"的类别是什么?等等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!