Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby对象模型澄清_Ruby_Metaprogramming - Fatal编程技术网

Ruby对象模型澄清

Ruby对象模型澄清,ruby,metaprogramming,Ruby,Metaprogramming,用Ruby,我可以写 Dog = Class.new 所以这里,Dog是一个对象,它是类的一个实例 Dog = Class.new fido = Dog.new 而且,我会写作 fido = Dog.new 这只有在狗是类时才可能发生 Dog = Class.new fido = Dog.new 这里的Dog是Class还是Object?ruby中的所有东西都是Object(块除外)。和狗这里还有一个类 Dog = Class.new fido = Dog.new 因此答案是:两者ru

用Ruby,我可以写

Dog = Class.new
所以这里,
Dog
是一个
对象
,它是
的一个实例

Dog = Class.new
fido = Dog.new
而且,我会写作

fido = Dog.new
这只有在
时才可能发生

Dog = Class.new
fido = Dog.new

这里的
Dog
Class
还是
Object

ruby中的所有东西都是
Object
(块除外)。和
这里还有一个

Dog = Class.new
fido = Dog.new

因此答案是:两者

ruby中的所有东西都是一个
对象
(块除外)。和
这里还有一个

Dog = Class.new
fido = Dog.new

因此,答案是:两者

询问对象本身,以了解它们属于何处,如下所示:

Dog = Class.new
fido = Dog.new
Dog.instance_of? Class #=> true
fido.instance_of? Class #=> false
fido.instance_of? Dog #=> true
Dog.superclass #=> Object
Dog.is_a? Object #=> true
Dog.is_a? Class #=> true

要更详细地查看,请参见

询问对象本身,以了解它们所属的位置,如下所示:

Dog = Class.new
fido = Dog.new
Dog.instance_of? Class #=> true
fido.instance_of? Class #=> false
fido.instance_of? Dog #=> true
Dog.superclass #=> Object
Dog.is_a? Object #=> true
Dog.is_a? Class #=> true

要更详细地了解,请参见

,我认为您犯了一些初学者反复犯的错误。您混淆了“is”的两种含义:

  • A是B的实例,并且
  • A是B的子类
你的案子

  • Dog
    类的实例,但不是
    类的子类,并且
  • Dog
    对象的子类,但不是
    对象的实例
因此,在不同的意义上,它是一个
,也是一个
对象


当他们说“Ruby中的一切都是
对象
”时,这并不意味着一切都是
对象
的实例。这意味着一切都是
对象的(自反)子类的一个实例

我认为你犯了一些初学者反复犯的错误。您混淆了“is”的两种含义:

  • A是B的实例,并且
  • A是B的子类
你的案子

  • Dog
    类的实例,但不是
    类的子类,并且
  • Dog
    对象的子类,但不是
    对象的实例
因此,在不同的意义上,它是一个
,也是一个
对象


当他们说“Ruby中的一切都是
对象
”时,这并不意味着一切都是
对象
的实例。这意味着一切都是
对象的(自反)子类的实例。首先,在命令提示符中键入
gem install y_support
,安装“y_support”。然后,在irb中:

require 'y_support/name_magic'

class Animal
  include NameMagic
end # You have created a new class
Animal.name #=> "Animal" -- the class is named Animal
class Dog < Animal
  def speak; puts "Bow wow!" end
end #=> hereby, you have created a subclass of Animal class
Cat = Class.new( Animal ) do
  def speak; puts "Meow!" end
end #=> this is another way of creating a subclass
Dog.name #=> "Dog" -- this is a class named Dog
让我们创建另一只狗:

Spot = Dog.new      #=> Another Dog instance
Dog.instances.size  #=> 2 -- we now have 2 dogs
Fido.class          #=> Dog -- Fido is an instance of class Dog
Spot.class          #=> Dog -- Spot is also an instance of class Dog
Fido.class.ancestors #=> The output shows you that Fido is also an Animal
Animal.instances.size #=> 3 -- together, we have 3 animals
Animal.instance_names #=> [:Fido, :Stripes, :Spot]
Animal.instance( :Fido ).speak #=> Bow wow!
Animal.instances.each &:speak #=> Bow wow! Meow! Bow wow!

明白了吗?请记住,在Ruby中,如果没有
NameMagic
就永远不能工作。首先,在命令提示符中键入
gem install y_support
来安装“y_support”。然后,在irb中:

require 'y_support/name_magic'

class Animal
  include NameMagic
end # You have created a new class
Animal.name #=> "Animal" -- the class is named Animal
class Dog < Animal
  def speak; puts "Bow wow!" end
end #=> hereby, you have created a subclass of Animal class
Cat = Class.new( Animal ) do
  def speak; puts "Meow!" end
end #=> this is another way of creating a subclass
Dog.name #=> "Dog" -- this is a class named Dog
让我们创建另一只狗:

Spot = Dog.new      #=> Another Dog instance
Dog.instances.size  #=> 2 -- we now have 2 dogs
Fido.class          #=> Dog -- Fido is an instance of class Dog
Spot.class          #=> Dog -- Spot is also an instance of class Dog
Fido.class.ancestors #=> The output shows you that Fido is also an Animal
Animal.instances.size #=> 3 -- together, we have 3 animals
Animal.instance_names #=> [:Fido, :Stripes, :Spot]
Animal.instance( :Fido ).speak #=> Bow wow!
Animal.instances.each &:speak #=> Bow wow! Meow! Bow wow!


明白了吗?请记住,在Ruby中,如果没有
NameMagic

它是类
class
的对象,就永远不能工作。所以,这是一门课。:)那么为什么Dog.superclass会产生Object呢?因为类就是对象。它是class
class
的对象。所以,这是一门课。:)那么为什么Dog.superclass会产生Object呢?因为类是对象。仍然无法理解一个东西怎么可能同时是类和对象,我需要这样假设吗?或者有什么资源可以让我进行更多的推理吗?@nik:几乎所有的东西——甚至是类——都是对象。@nik:狗是哺乳动物。菲多是一只狗。所以,它既是狗又是哺乳动物。层次结构的不同层次。每次创建某个对象时,它都是该类的一个实例。如果您获取一个类
class
并创建它的实例,您将拥有一个
class
对象(一个
class
的实例)。就像你的
类一样,当你创建它的实例时,你有一个
对象的实例。仍然无法理解一个事物怎么能同时是类和对象,我需要这样假设吗?或者有什么资源可以让我进行更多的推理吗?@nik:几乎所有的东西——甚至是一个类——都是一个对象。@nik:狗是哺乳动物。菲多是一只狗。所以,它既是狗又是哺乳动物。层次结构的不同层次。每次创建某个对象时,它都是该类的一个实例。如果您获取一个类
class
并创建它的实例,您将拥有一个
class
对象(一个
class
的实例)。就像你的
Dog
类一样,当你创建它的一个实例时,你有一个
Dog
对象的实例。注意:LSP几乎要求“是B的一个子类”与“是B的一个实例”无法区分,至少从任何期望B的人的角度来看。所有对象都是
Object
及其特定类型的实例,以及继承链上每个类的实例。这就是为什么
instance\u of?
is\u a?
在涉及子类时返回true。instance\u of?不考虑子类。什么样的?是的。我已经有一段时间没有搞乱Ruby了,显然误读了Priti的例子:P不过,你对“是”的第二个意思似乎不起作用。比如说,狗是哺乳动物的一个子类。不管这种关系如何,“
是哺乳动物”是不正确的
rover
,然而,作为狗的一个实例,它是狗、哺乳动物和物体<由于
Class
扩展
Object
,code>Dog
同样是一个类和一个对象。注意:LSP几乎要求“是B的子类”与“是B的实例”无法区分,至少从任何期望B的人的角度来看。所有对象都是
Object
及其特定类型的实例,以及继承链上每个类的实例。这就是为什么在涉及子类时,
的实例返回true的原因