Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between class Foo; end & Foo = Class.new (And module/Module)?

Tags:

semantics

ruby

I spent a while debugging a problem where I had made a typo of using Module instead of module to declare a module. Why does Ruby have module and Module (and similarly class and Class), with different semantics? See example below:

class C; end; # ok
Class C2; end; # error
C3 = class.new # error
C4 = Class.new # ok

It seems confusing to have two different constructs which differ only by case.

like image 357
Anand Avatar asked Dec 13 '25 17:12

Anand


1 Answers

class Foo; end is roughly the same as Foo = Class.new, but there are some key differences. All of these also hold true for module Bar; end vs. Bar = Module.new.

  • class is a keyword whereas Class is simply a constant name like any other.

  • Class.new allows creating anonymous (unnamed) classes, whereas class; end is a syntax error. This makes the former more useful for metaprogramming.

  • class creates new lexical scope whereas Class.new do ... end does not. E.g.:

    class Foo; BAR = 42; end
    Baz = Class.new { QUX = 42 }
    BAR       #   NameError: uninitialized constant BAR
    Foo::BAR  #=> 42
    QUX       #=> 42
    Baz::QUX  #=> 42  (warning: toplevel constant QUX referenced by Baz::QUX)
    
  • Since Class.new do ... end is a closure, you can close around local variables. class ... end cannot. E.g.

    foo = 42
    class Foo; puts foo; end  # NameError: undefined local variable or method `foo' for Foo:Class
    Class.new { puts foo }    # (prints 42)
    
  • class names the class immediately, whereas Class.new does it on assignment (last).

  • Class.new cannot be used to reopen an existing class (class_exec or similar must be used), whereas class Foo; end reopens Foo if it already exists and is a Class.

Besides the functional differences, the keyword syntax is arguably easier to read akin to {} vs. Hash.new and [] vs. Array.new.

like image 101
Andrew Marshall Avatar answered Dec 15 '25 13:12

Andrew Marshall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!