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.

> > 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.

> 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.

a=nil
(a==a).class
#=> TrueClass

>
> The real danger is that you start testing 'truth' with expressions
> like x == true or x == false.  As long as you don't do that and simply
> use the value in conditional expressions like
>   if x ...
>   x ? ... : ...
> and the like you'll be fine.
>
> You rarely need an actual true/false value in Ruby.

To newbies, this statement doesn't make much sense, but I do
understand where you're coming from.  Newbies should definitely learn
that in a conditional, thinks are not always what they seem (and I
love it!).

a = 1
b = 2
a if b

> For more on this see
> http://www.therailsway.com/2007/8/1/dangers-of-cargo-culting

I didn't know this acquired the euphemism "cargo-culting".  I still
thought it was called "script kiddies".  I must be getting old :-)

> --
> Rick DeNatale
>
> My blog on Ruby
> http://talklikeaduck.denhaven2.com/
>
>

Todd