Project

General

Profile

Actions

Feature #20712

closed

module should return newly defined module

Added by esad (Esad Hajdarevic) 3 months ago. Updated 3 months ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:119023]

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
Actions #1

Updated by esad (Esad Hajdarevic) 3 months ago

  • Description updated (diff)

Updated by esad (Esad Hajdarevic) 3 months 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) 3 months 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.

Actions #4

Updated by nobu (Nobuyoshi Nakada) 3 months ago

  • Status changed from Open to Closed
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0