Booleans

M

Mark J. Reed

Okay, as a convert from Perl to Ruby, I have to say that I love
just about everything Ruby has to offer. Just about. I wish it had
better built-in support for Unicode, but that's a minor quibble.
However, one thing I really don't like is the handling of booleans.

I would be happy if the conditionals required actual Boolean
(i.e., TrueClass or FalseClass) objects and complained otherwise, even
though it's rather different from the typeless nature of the rest
of Ruby.

I would also be happy if the test for falseness was less simplistic than
"false is false, nil is false, everything else is true". I am constantly
being bitten by the fact that 0 is true.

Ideally, what I'd like to see is the ability for each class to decide what
constitutes true and false for objects of that class - for instance, a
to_b method, by analogy with to_s. Then you just need three method
definitions:

class Object; def to_b() true end end
class FalseClass; def to_b() false end end
class NilClass; def to_b() false end end

And boom, you have the current behavior and the ability to extend it.
For instance, if you prefer that zero be false, you can do

class Numeric; def to_b() self != 0 end end

Admittedly, this would break the semantics of =~ etc, but you could
fix that with a new class for match results that still has 0 as true:

class Index < Fixnum; def to_b() true end end

Similar methods would allow empty strings and collections to return
false, etc. The point is that it would gain a lot of flexibility.
And a static method like this:

def TrueClass.strictlyTrue?(x) x != nil && x != false end

would allow the user to override any given class's definitions to
make sure that an object is "really" true.

Thoughts?

-Mark
 
G

Gennady

----- Original Message -----
From: "KONTRA Gergely" <[email protected]>
To: "ruby-talk ML" <[email protected]>
Sent: Monday, July 14, 2003 1:45 PM
Subject: Re: Booleans

What about not automagically converting everything to boolean?

Then the following nice feature would not be possible:

@var ||= SomeClass.new
Gergo
--
+-[ Kontra, Gergely<[email protected]> PhD student Room IB113 ]---------+
| http://www.mcl.hu/~kgergely "Olyan langesz vagyok, hogy |
| Mobil:(+36 20) 356 9656 ICQ: 175564914 poroltoval kellene jarnom" |
+-- Magyar php mirror es magyar php dokumentacio: http://hu.php.net --+
 
M

Mark J. Reed

If you *know* that you will be getting a Fixnum in response,
you can use Fixnum#nonzero? and Fixnum#zero? to get the tests that
you're after.

Sure. Or just plain old != 0. But I always forget that I need to do
anything at all beyond "if (expression returning Numeric)". After
20 years programming C and derivatives, I have simply internalized
that zero is SUPPOSED to be false, dagnabit!

But I do think it would be worthwhile to allow classes to specify
their own truth values, even if Ruby as shipped doesn't take advantage
of this feature.

-Mark
 
D

Daniel Berger

Mark J. Reed said:
Sure. Or just plain old != 0. But I always forget that I need to do
anything at all beyond "if (expression returning Numeric)". After
20 years programming C and derivatives, I have simply internalized
that zero is SUPPOSED to be false, dagnabit!

But I do think it would be worthwhile to allow classes to specify
their own truth values, even if Ruby as shipped doesn't take advantage
of this feature.

-Mark

That was part of the reason I submitted RCR #143 (on RubyGarden)
which, unfortunately, was rejected. Care to revive it?

Dan
 
T

ts

M> my brain can step in and think "But what is Truth? Is Truth
M> unchanging law?"


pigeon% perldoc perlfunc
[...]
fcntl FILEHANDLE,FUNCTION,SCALAR
[...]

You don't have to check for "defined" on the return from
"fnctl". Like "ioctl", it maps a 0 return from the system call
into "0 but true" in Perl.
^^^^^^^^^^^^
[...]
pigeon%


Guy Decoux
 
B

Brian Candler

M> my brain can step in and think "But what is Truth? Is Truth
M> unchanging law?"


pigeon% perldoc perlfunc
[...]
fcntl FILEHANDLE,FUNCTION,SCALAR
[...]

You don't have to check for "defined" on the return from
"fnctl". Like "ioctl", it maps a 0 return from the system call
into "0 but true" in Perl.
^^^^^^^^^^^^

Good point. Certain perl functions return "0E" when they mean "0", so that
it can be treated as 'true'.

Just remember that Ruby isn't Perl or C. It doesn't take long to get used to
it, and you'll find it's much cleaner. After all, why should the number "0"
be treated differently from the number "1"?

Regards,

Brian.
 
H

Hal E. Fulton

----- Original Message -----
From: "Mark J. Reed" <[email protected]>
Newsgroups: comp.lang.ruby
To: "ruby-talk ML" <[email protected]>
Sent: Tuesday, July 15, 2003 7:16 AM
Subject: Re: Booleans

Ok, see, I have absolutely no problem understanding the concept.
Easy as pie. I know that 0 is true, and I know why it is true.
But a couple decades of programming habits step in and write things
(like "if n" where I intended "if n==0") on my behalf long before
my brain can step in and think "But what is Truth? Is Truth
unchanging law?"

Don't forget to wash your hands... ;)

Hal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top