Booleans

Discussion in 'Ruby' started by Mark J. Reed, Jul 14, 2003.

  1. Mark J. Reed

    Mark J. Reed Guest

    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
     
    Mark J. Reed, Jul 14, 2003
    #1
    1. Advertising

  2. Mark J. Reed

    Gennady Guest

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


    > On 0715, Mark J. Reed wrote:
    > > 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.

    > What about not automagically converting everything to boolean?


    Then the following nice feature would not be possible:

    @var ||= SomeClass.new

    >
    > Gergo
    > --
    > +-[ Kontra, Gergely<> 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 --+
    >
    >
     
    Gennady, Jul 14, 2003
    #2
    1. Advertising

  3. Mark J. Reed

    Mark J. Reed Guest

    On Tue, Jul 15, 2003 at 06:17:46AM +0900, Austin Ziegler wrote:
    > On Tue, 15 Jul 2003 05:34:02 +0900, Mark J. Reed wrote:
    > >?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.

    >
    > 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
     
    Mark J. Reed, Jul 14, 2003
    #3
  4. "Mark J. Reed" <> wrote in message news:<>...
    > On Tue, Jul 15, 2003 at 06:17:46AM +0900, Austin Ziegler wrote:
    > > On Tue, 15 Jul 2003 05:34:02 +0900, Mark J. Reed wrote:
    > > >?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.

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


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

    Dan
     
    Daniel Berger, Jul 15, 2003
    #4
  5. Mark J. Reed

    ts Guest

    >>>>> "M" == Mark J Reed <> writes:

    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
     
    ts, Jul 15, 2003
    #5
  6. On Tue, Jul 15, 2003 at 09:25:08PM +0900, ts wrote:
    > >>>>> "M" == Mark J Reed <> writes:

    >
    > 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.
     
    Brian Candler, Jul 15, 2003
    #6
  7. ----- Original Message -----
    From: "Mark J. Reed" <>
    Newsgroups: comp.lang.ruby
    To: "ruby-talk ML" <>
    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
     
    Hal E. Fulton, Jul 15, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. array of booleans

    , Oct 1, 2005, in forum: Java
    Replies:
    4
    Views:
    550
  2. Martin Herbert Dietze

    g++: integers as booleans, no warning?

    Martin Herbert Dietze, Feb 17, 2005, in forum: C++
    Replies:
    9
    Views:
    481
    Karl Heinz Buchegger
    Feb 18, 2005
  3. Roman Suzi

    Booleans and comparison results

    Roman Suzi, Jun 24, 2003, in forum: Python
    Replies:
    0
    Views:
    713
    Roman Suzi
    Jun 24, 2003
  4. =?ISO-8859-1?Q?Gerhard_H=E4ring?=

    Re: Booleans and comparison results

    =?ISO-8859-1?Q?Gerhard_H=E4ring?=, Jun 24, 2003, in forum: Python
    Replies:
    0
    Views:
    700
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=
    Jun 24, 2003
  5. Gerrit Holl

    Re: Booleans and comparison results

    Gerrit Holl, Jun 24, 2003, in forum: Python
    Replies:
    1
    Views:
    665
    Greg Ewing (using news.cis.dfn.de)
    Jun 25, 2003
Loading...

Share This Page