Project

General

Profile

Actions

Bug #20943

open

Constant defined in `Data.define` block

Added by nobu (Nobuyoshi Nakada) 8 days ago. Updated about 8 hours ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:120174]

Description

From https://github.com/ruby/ruby/pull/12274:

A couple times in code review I've seen constants inadvertently leak to top level from within a Struct or Data do block. I think it would be nice to show reopening the Data class when a constant is defined, so the constant is defined within the namespace. In this case, Measure::NONE instead of top level Object::NONE. It would also show readers that it's okay to reopen a Data class, which seems nice since some folk might not realize. Thanks for considering!

However, I think that NONE probably might be intended to be defined under Measure.

Current:

Measure = Data.define(:amount, :unit) do
  NONE = Data.define
end
p NONE #=> NONE

Another:

Measure = Data.define(:amount, :unit) do
  NONE = Data.define
  p NONE #=> Measure::NONE
end
p NONE # uninitialized constant NONE (NameError)

@zverok (Victor Shepelev) How do think?

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0