From: craig duncan Date: 2000-12-23T12:57:09+09:00 Subject: [ruby-talk:7988] Re: Naming of "defined?" Yukihiro Matsumoto wrote: > > Hi, > > In message "[ruby-talk:7961] Naming of "defined?"" > on 00/12/23, craig duncan writes: > > |Didn't i read that '?' at the end of a method name indicated a boolean > |return value? > > The '?' methods are methods designed to be used as predicates, not > methods which CAN be used as predicate. They return either > > (a) true or false > (b) non-false informative value or nil > > `defined?' falls into (b). > > In addition, as a rare exception, Thread#alive? returns true for > unterminated threads, false for normally terminated threads, and nil > for threads terminated by exceptions. > > matz. I made a mistake in thinking there was something wrong with the `not defined?' predicate in: craig@duncan:/usr/home/craig/ruby$ cat t.rb #!/usr/bin/ruby def i p "defining i as method" end p defined? i i = 0 if not defined? i p defined? i craig@duncan:/usr/home/craig/ruby$ t.rb "method" "local-variable" I assumed that "i = 0" must have been executed because something that _had_ been defined as a method was now seen as a local-variable. But that's not what happened. The predicate _did_ fail but just the appearance of "i = 0" (not executed) made the method "disappear". So i'm just trying to understand some of the quirky aspects of this. I thought that if a later _assignment_ of a variable could override a method definition, then a later method definition could override a variable "declaration". But (i see) that's not exactly what happened and . . . it doesn't work that way. It's one (and only one) or the other, but the interpreter can change it's mind about which one it is -- variables seeming to have preference over methods this way. There are instances when a symbol is assumed to be a method and then found to actually be a variable reference. Is there any point, though, in allowing methods to be redefined as variables? I would think that (as suggested) warnings in the former case could be issued and, in the latter, it would be an error. But then, i've only been playing with Ruby for a couple of days and maybe this behavior is actually useful. craig