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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With