Feature #20712
closedmodule should return newly defined module
Description
Currently module keyword returns nil after defining a new module:
x = module Foo
end
# x is nil
It would be more consistent with Module.new if module keyword returned the newly defined module. Then we could do:
using(module Foo
refine ...
end)
instead of
module Foo
end
using Foo
Updated by esad (Esad Hajdarevic) about 1 year ago
- Description updated (diff)
Updated by esad (Esad Hajdarevic) about 1 year ago
Ok I was too quick on this one. module keyword returns last expression in the block, so just ending module declaration with self works:
using(module Foo
refine ...
self
end)
so this request can be closed
Updated by ufuk (Ufuk Kayserilioglu) about 1 year ago
module and class don't return nil, they return the value of the last statement in the scope:
x = module Foo
42
end
puts x #=> 42
x = class Bar
def foo = 42
end
puts x #=> :foo
There might be code that relies on this fact, so the proposal could be a backward compatibility concern.
On the other hand, if this were to change, since method definitions (i.e. defs) return the name of the method defined and not the method object itself, I think it would make more sense for module/class to return the name of the module/class being defined and not the module/class object itself.
Updated by nobu (Nobuyoshi Nakada) about 1 year ago
- Status changed from Open to Closed