Reputation: 379
Can anybody explain where does Config, resp. RbConfig, comes from for the NilClass f.E. ?
irb> NilClass::Config # RbConfig
irb> NilClass.constants # [] ??
Update:
Can not find out how is the inheritance applied here. Why I can access a top-level constant with an Object's sublass but cann't get its symbol by invoking constants
method ?
Upvotes: 1
Views: 151
Reputation: 95252
It's a top-level constant. You can access it through another module, but it doesn't actually live there. Class::RbConfig
, Fixnum::RbConfig
, etc, all work, but the constant itself only lives at the top level.
You can find it in Object.constants
. Object::RbConfig
is the only fully-qualified way of referencing it that doesn't result in a 'toplevel constant referenced by Module::' warning.
EDIT: Normally, inherited constants do show up in the value of .constants
, but the ones from Object
seem to be excluded:
irb(main):001:0> Object.constants
=> [:Object, :Module, :Class, :BasicObject, :Kernel, :NilClass, :NIL, :Data, :TrueClass, :TRUE, :FalseClass, :FALSE, :Encoding, :Comparable, :Enumerable, :String, :Symbol, :Exception, :SystemExit, :SignalException, :Interrupt, :StandardError, :TypeError, :ArgumentError, :IndexError, :KeyError, :RangeError, :ScriptError, :SyntaxError, :LoadError, :NotImplementedError, :NameError, :NoMethodError, :RuntimeError, :SecurityError, :NoMemoryError, :EncodingError, :SystemCallError, :Errno, :ZeroDivisionError, :FloatDomainError, :Numeric, :Integer, :Fixnum, :Float, :Bignum, :Array, :Hash, :ENV, :Struct, :RegexpError, :Regexp, :MatchData, :Marshal, :Range, :IOError, :EOFError, :IO, :STDIN, :STDOUT, :STDERR, :ARGF, :FileTest, :File, :Dir, :Time, :Random, :Signal, :Process, :Proc, :LocalJumpError, :SystemStackError, :Method, :UnboundMethod, :Binding, :Math, :GC, :ObjectSpace, :Enumerator, :StopIteration, :RubyVM, :Thread, :TOPLEVEL_BINDING, :ThreadGroup, :Mutex, :ThreadError, :Fiber, :FiberError, :Rational, :Complex, :RUBY_VERSION, :RUBY_RELEASE_DATE, :RUBY_PLATFORM, :RUBY_PATCHLEVEL, :RUBY_REVISION, :RUBY_DESCRIPTION, :RUBY_COPYRIGHT, :RUBY_ENGINE, :ARGV, :Gem, :RbConfig, :Config, :CROSS_COMPILING, :Exception2MessageMapper, :IRB, :RubyToken, :RubyLex, :Readline, :Date]
irb(main):002:0> class Parent < Object; Answer = 42; end
=> 42
irb(main):003:0> class Child < Parent; end
=> nil
irb(main):004:0> Child.constants
=> [:Answer]
You may wish to read this article about constant lookups in Ruby. This passage seems relevant:
Ruby assumes that you will mix modules into something that inherits from Object. So if the currently open module is a module, it will also add Object.ancestors to the lookup chain so that top-level constants work as expected.
Upvotes: 2