James Britt
11/16/2007 2:23:00 AM
Todd Benson wrote:
> On Nov 15, 2007 5:28 PM, Rick DeNatale <rick.denatale@gmail.com> wrote:
>> On Nov 15, 2007 5:30 PM, James Britt <james.britt@gmail.com> wrote:
>>> Suraj Kurapati wrote:
>>>> So, forcing question-mark methods to return only 'true' and 'false'
>>>> feels far too restrictive and seems to follow a
>>>> there's-only-one-way-to-do-it philosophy IMHO.
>>> Forcing would be bad, but there is the convention that foo? returns true
>>> vs. false, not something vs. nil.
>> There is no such convention in Ruby.
>
> No convention in Ruby, but in Ruby programmers.
Right. And in a TIMTOWTDI language is can amount to the same thing.
>
>>> If a foo? method happens to be returning something other than TrueClass,
>>> and you start depending on that quirk, you run the risk that a later
>>> version of that method returns some other non-nil/non-false object.
>> There ARE cases in the ruby language where a x? returns something
>> other than true or false
>
> Yes, that's great! No forcing, but just realizing that for your code
> to work with others' requires some establishment of convention. Ruby
> is a weird and happy monster that way. You can do almost anything
> with it, but then when you want to do something with it, you start
> setting down ground rules in order to play well with others.
Exactly. Yes, there are exceptions, and yes that can be handy, but
(culturally speaking) treating foo? as an attribute accessor is probably
a mistake because you may be relying on a quirk of implementation.
Since *every* method could be used as a boolean value, the use of '?'
should be reserved to expressed a particular meaning, i.e., all this
method assures you is that you'll get a return value than can be
interpreted as true or false, and is not meant to be an object accessors.
>
>> defined?(a) # => nil
>> a = 1
>> defined?(a) # => "local-variable"
>>
>> By the way, you really meant true instead of TrueClass right?
>
> Well, that's kind of just semantics. James may have meant an instance
> of TrueClass, but didn't word it that way.
Yes, thank you. I meant not simply true (i.e., something that is not
nil), but "only" true (and not some particular object you are relying on
to have a secondary value).
--
James Britt
"Programs must be written for people to read, and only incidentally
for machines to execute."
- H. Abelson and G. Sussman
(in "The Structure and Interpretation of Computer Programs)