From: "shan (Shannon Skipper) via ruby-core" Date: 2024-12-17T23:51:21+00:00 Subject: [ruby-core:120284] [Ruby master Bug#20943] Constant defined in `Data.define` block Issue #20943 has been updated by shan (Shannon Skipper). byroot (Jean Boussier) wrote in #note-2: > Which is a bit wasteful as you define two classes instead of one, but not a big deal. I agree the extra class being created is minor, but I'm slightly more bothered by the anonymous class in the ancestry. ```ruby [Something, #, Struct, ... ] ``` > I kinda wish this would be valid syntax: > > ```ruby > class Something = Struct.new(:a, :b) > ... > end > ``` I'd like that too. That same pattern would be great for both `Struct` and `Data` from my vantage. Or if both could keep constants in scope within `do` blocks, but would that be consider breaking? Too late for Data? I'd like it. It seems like options include: 1. Change `Data.define do` block scope for constants 2. Add a new `Something = Data.define` or similar syntax 3. Recommend reopening classes in docs 4. Recommend inheritance in docs 5. Keep the status quo of defining a constant outside the scope in docs That ^ list happens to be roughly in my personal order of preference from top to bottom. :) Having both 1 and 2 would also be an option. I wonder if changing documentation to something that keeps `NONE` inside the module is worth doing in the short term? If syntax adjustments are decided against, I'd rather just recommend reopening `Data` and `Struct` classes rather than the slight back bending with existing solutions like `self::` prefix or `< Data.define`. On the other hand, I'd love to see a syntax adjustment to make it easier to define a constant within a `Data` without reopening the class. ---------------------------------------- Bug #20943: Constant defined in `Data.define` block https://bugs.ruby-lang.org/issues/20943#change-111049 * Author: nobu (Nobuyoshi Nakada) * Status: Open * Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- >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: ```ruby Measure = Data.define(:amount, :unit) do NONE = Data.define end p NONE #=> NONE ``` Another: ```ruby Measure = Data.define(:amount, :unit) do NONE = Data.define p NONE #=> Measure::NONE end p NONE # uninitialized constant NONE (NameError) ``` @zverok How do think? -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/