class vs Class.new, module vs Module.new

前端 未结 2 513
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-11 19:07

What is difference between class and Class.new & module and Module.new?

I know that:

  1. C

2条回答
  •  Happy的楠姐
    2020-12-11 19:39

    Class.new returns an object. class A returns nil. Same goes for modules.

    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:

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

    In older versions of Ruby, the return value of a method definition expression was implementation-defined. Rubinius returned a CompiledMethod object for the method in question, whereas YARV and most others simply returned nil.

提交回复
热议问题