From: Peter Date: 2003-09-20T07:48:10+09:00 Subject: Re: Defining constants in global scope > My guess is, and I'm new to Ruby so I'm probably wrong, that you first > need to open the "global" Module or class for which you want to define > the constant for. In this case this would be the Kernel module. Look at the transscript below. Apparently toplevel constants don't really belong to the Kernel module. However if a constant is defined at the toplevel, and doesn't clash with a constant in the Kernel module, you can access it in the Kernel module, but you get a warning. Also toplevel constants can be read as ::MyConstant, but not assigned. Somehow this behavior seems a bit inconsistent to me. Also I would never allow access to toplevel constants through the Kernel module because if you use someone else's code which defines a constant with the same name in the Kernel module (should someone ever want to do such a thing), you're in for one big bug hunting safari. But that's why it gives a warning I guess, but it's human nature to ignore warnings, especially when the code still works... Peter irb(main):001:0> MyConstant = 1 => 1 irb(main):002:0> MyConstant => 1 irb(main):003:0> ::MyConstant => 1 irb(main):004:0> Kernel::MyConstant (irb):4: warning: toplevel constant MyConstant referenced by Kernel::MyConstant => 1 irb(main):005:0> Kernel::MyConstant = 2 => 2 irb(main):006:0> MyConstant => 1 irb(main):007:0> ::MyConstant = 2 SyntaxError: compile error (irb):7: syntax error ::MyConstant = 2 ^ from (irb):7 irb(main):008:0> Kernel::MyConstant => 2