简体   繁体   English

class vs Class.new,模块vs Module.new

[英]class vs Class.new, module vs Module.new

What is difference between class and Class.new & module and Module.new ? classClass.newmoduleModule.new什么Module.new

I know that: 我知道:

  1. Class.new / Module.new create an anonymous class / module . Class.new / Module.new创建一个匿名class / module When we assign it to constant for the first time it becomes name of that class / module . 当我们第一次将其分配给常量时,它将成为class / module名称。 class / module do this automatically. class / module会自动执行此操作。

  2. When we want to inherit, we can pass an argument: Class.new(ancestor) . 当我们想要继承时,我们可以传递一个参数: Class.new(ancestor) When we don't specify an ancestor, it is set to the Object . 如果不指定祖先,则将其设置为Object class use this syntax: class A < Ancestor class使用以下语法: class A < Ancestor

  3. Class.new returns an object . Class.new返回一个object class A returns nil . class A返回nil Same goes for module s. module s也是如此。

Did I miss something? 我错过了什么?

The interesting point that you missed between class keyword and Class::new is - Class::new accepts block. 您在class关键字和Class::new之间错过的一个有趣的地方是Class::new accepts块。 So when you will be creating a class object using Class::new you can also access to the surrounding variables. 因此,当您使用Class::new创建类对象时,您也可以访问周围的变量。 Because block is closure . 因为封闭的 But this is not possible, when you will be creating a class using the keyword class . 但是,当您使用关键字class创建类时,这是不可能的。 Because class creates a brand new scope which has no knowledge about the outside world. 因为class创造了一个崭新的范围,对外部世界一无所知。 Let me give you some examples. 让我给你一些例子。

Here I am creating a class using keyword class : 在这里,我使用关键字class创建一个class

count = 2

class Foo
    puts count
end
# undefined local variable or method `count' for Foo:Class (NameError)

Here one using Class.new : 这里使用Class.new

count = 2
Foo = Class.new do |c|
    puts count
end
# >> 2

The same difference goes with keyword module and Module::new . 关键字moduleModule::new具有相同的区别。

Class.new returns an object . Class.new返回一个object class A returns nil . class A返回nil Same goes for module s. module s也是如此。

That's wrong. 错了 A class/module definition returns the value of the last expression evaluated inside of the class/module body: 类/模块定义返回在类/模块主体内部求值的最后一个表达式的值:

class Foo
  42
end
# => 42

Typically, the last expression evaluated inside of a class/module body will be a method definition expression, which in current versions of Ruby returns a Symbol denoting the name of the method: 通常,在类/模块主体内部求值的最后一个表达式将是方法定义表达式,在当前版本的Ruby中,该方法定义表达式将返回一个表示方法名称的Symbol

class Foo
  def bar; end
end
# => :bar

In older versions of Ruby, the return value of a method definition expression was implementation-defined. 在旧版本的Ruby中,方法定义表达式的返回值是实现定义的。 Rubinius returned a CompiledMethod object for the method in question, whereas YARV and most others simply returned nil . Rubinius为有问题的方法返回了CompiledMethod对象,而YARV和其他大多数对象仅返回nil

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

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