Skip to content

Object.constants is empty (Object.constants(false) is not) #833

Closed

Description

@tannevaled

On rbgo v0.8.3 (-tags rbgo_core, through go-scikit/ruby v0.1.2), Object.constants is empty while Object.constants(false) and Module.constants are not:

class Foo; end
Object.constants.size          # => 0      (MRI: several hundred)
Object.constants(true).size    # => 0
Object.constants(false).size   # => 377
Module.constants.include?(:Foo) # => true
Object.const_defined?(:Stats)  # => true
Kernel.constants.size          # => 0

In MRI, Module#constants(inherit = true) on Object returns Object's own constants plus those of its ancestors; here the inherited form seems to skip the receiver's own table.

Found while writing go-scikit/ruby's interactive completion (go-scikit/ruby#8), which uses Module.constants instead for now.

🤖 Generated with Claude Code

Activity

  1. tannevaled commented on Oct 31, 2026

    @tannevaled
    ContributorAuthor

    Fixed in v0.8.4 (PR #840).

    Module#constants(inherit=true) always skipped Object and BasicObject, so Object.constants was empty. It now follows MRI's rb_mod_const_of: the walk stops before Object unless the receiver is Object. Verified at the tag: Object.constants lists every top-level constant (core and user-defined), Kernel.constants and the inherited form work, private constants are not listed, and Module.constants with no argument walks the caller's lexical scope. Performance: 0.33 ms for 10 000 constants on one core, level with MRI 3.4.

    Order differs from MRI only inside one table, where MRI's order is unspecified and rbgo sorts.

    🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions