From: daz Date: 2004-02-16T15:59:56+09:00 Subject: Re: accessing constants from class methods > David A. Black wrote: > > >OK, it's probably something simple but I'm staring and staring at the > >original code, and its output (which is the same as daz's code), and I > >can't figure out why it wasn't OK. > > > >Explanations welcome :-) > > > Your post didn't get through to the NG, David. Ara probably didn't see it and there appeared to be no replies when I posted. http://groups.google.com/groups?hl=en&th=5677a0c1234ad853 I'm wary about the defined? keyword -- I can't find a good definition of what service it provides. for i in 0..1 # (avoiding new scope) puts defined? a a = 1 puts defined? a puts '-' * 10 end #-> nil #-> local-variable #-> ---------- #-> nil #-> local-variable #-> ---------- ## Strangely, for constants it works differently. for i in 0..1 puts defined? X X = 1 puts defined? X puts '-' * 10 end #-> nil #-> constant #-> ---------- #-> constant #-> constant #-> ---------- ## The next test made me wonder if I can believe anything, any more. for i in 0..1 puts defined? Z const_set Z, 1 puts defined? Z puts '-' * 10 end #-> nil #-> C:/TEMP/rb3273.TMP:17: uninitialized constant Z (NameError) #-> from C:/TEMP/rb3273.TMP:15:in `each' #-> from C:/TEMP/rb3273.TMP:15 =begin --------------------------------------------------------------- Module#const_set mod.const_set( aSymbol, anObject ) -> anObject -------------------------------------------------------------------------------- Sets the named constant to the given object, returning that object. Creates a new constant if no constant with the given name previously existed. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [...] =end daz