Module constant access

I found this to be an odd behavior.

Do module constants live in a different table than that which
const_defined? uses?

irb(main):013:0> module Trouble
irb(main):014:1>
irb(main):015:1* end
=> nil

irb(main):022:0> Module.const_defined? “Trouble”
=> false
irb(main):023:0> Module.constants.include? “Trouble”
=> true
irb(main):024:0> Object.const_defined? “Trouble”
=> true
irb(main):025:0> Object.constants.include? “Trouble”
=> true

Dan

irb(main):013:0> module Trouble

When you write this, this is like if you write

   Trouble = Module.new

Guy Decoux

Sure. This makes sense. But why the difference in visibility of the
constant Trouble?

Dan

···

On Wednesday, May 21, 2003, at 10:25 America/New_York, ts wrote:

irb(main):013:0> module Trouble

When you write this, this is like if you write

Trouble = Module.new

Hi –

irb(main):013:0> module Trouble

When you write this, this is like if you write

Trouble = Module.new

Sure. This makes sense. But why the difference in visibility of the
constant Trouble?

It’s not exactly visibility – const_defined? tells you whether that
constant was/is defined in that module, not just whether the module
can see it. For example:

irb(main):001:0> class A; X = 1; end
=> 1
irb(main):002:0> class B < A; end
=> nil
irb(main):003:0> B::X
=> 1
irb(main):004:0> A.const_defined?(“X”)
=> true
irb(main):005:0> B.const_defined?(“X”)
=> false
irb(main):006:0> class B; X = 2; end
=> 2
irb(main):007:0> B.const_defined?(“X”)
=> true

David

···

On Wed, 21 May 2003, Dan Janowski wrote:

On Wednesday, May 21, 2003, at 10:25 America/New_York, ts wrote:


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Sure. This makes sense. But why the difference in visibility of the
constant Trouble?

I don't understand, you want to say the difference between
Module::const_defined? and Module::constants

svg% ri const_defined?
-------------------------------------------------- Module#const_defined?
     mod.const_defined?( aSymbol ) -> true or false

···

------------------------------------------------------------------------
     Returns true if a constant with the given name is defined by mod.
        Math.const_defined? "PI" #=> true

svg%

svg% ri Module::constants
------------------------------------------------------ Module::constants
     Module.constants -> anArray
------------------------------------------------------------------------
     Returns an array of the names of all constants defined in the
     system. This list includes the names of all modules and classes.
        p Module.constants.sort[1..5]
     produces:
        ["ARGV", "ArgumentError", "Array", "Bignum", "Binding"]

svg%

Guy Decoux