[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Booleans

Ben Giddings

7/14/2003 10:18:00 PM

On Mon July 14 2003 5:17 pm, 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.

I don't think the issue is that it is difficult to come up with a test to
determine whether or not a number is zero or nonzero, it's just whether
Ruby's treatment of "false" and "nil" as false and 0 and the empty string as
true makes sense, or if it should maybe be more flexible.

I happen to like 0 and "" being logically "true". It sometimes requires extra
typing but that's fine. It's very lisp-ey in that only nil is false,
everything else is true, only Ruby does have boolean classes, and in that
case the literal "false" should of course be false.

While I also kind-of like the idea of a "to_b" method that can determine if a
class is logically false, I see it causing lots of problems. If that were
possible, someone might take advantage of Ruby's flexibility to make 0.to_b
be false for their program. This could lead to some real difficulty in
understanding or running other people's code.

I prefer to be overly anal about the whole thing and always just say:
if 0 != retval ...

Ben


4 Answers

george.marrows

7/15/2003 10:01:00 AM

0

> While I also kind-of like the idea of a "to_b" method that can determine if a
> class is logically false, I see it causing lots of problems. If that were
> possible, someone might take advantage of Ruby''s flexibility to make 0.to_b
> be false for their program. This could lead to some real difficulty in
> understanding or running other people''s code.

You can already override parts of Ruby to cause plenty of odd
behaviour:

class Fixnum
def + (y)
1
end
end

puts "2 + 2 = #{2 + 2}"

I think Matz has commented on this topic before and said that the
reason that only nil and false are logically false is for performance.
The test for an object being nil or false is a very quick. If logical
tests instead had to call .to_b then that''d be an extra message send
on every if, and, or, when, ...

-- George

Robert Klemme

7/15/2003 11:48:00 AM

0


"George Marrows" <george.marrows@ps.ge.com> schrieb im Newsbeitrag
news:5e44d1a4.0307150201.7b1a6f08@posting.google.com...
> > While I also kind-of like the idea of a "to_b" method that can
determine if a
> > class is logically false, I see it causing lots of problems. If that
were
> > possible, someone might take advantage of Ruby''s flexibility to make
0.to_b
> > be false for their program. This could lead to some real difficulty
in
> > understanding or running other people''s code.
>
> You can already override parts of Ruby to cause plenty of odd
> behaviour:
>
> class Fixnum
> def + (y)
> 1
> end
> end
>
> puts "2 + 2 = #{2 + 2}"

That''s true, but why should we add another option to shoot ourselves in
the foot? I regard the to_b thing more serious than the changed operator
+ that you present: logical values always have to do with the way an
algorithm executes, while a different return value for + is still a number
and will only have minor impacts in most cases. Especially think of
someone changing 0.to_b to return false and then using a module of someone
else that relied on 0 being true...

> I think Matz has commented on this topic before and said that the
> reason that only nil and false are logically false is for performance.
> The test for an object being nil or false is a very quick. If logical
> tests instead had to call .to_b then that''d be an extra message send
> on every if, and, or, when, ...

That''s a serious reason, too.

Kind regards

robert

navy.internetoplichting

12/24/2007 8:26:00 AM

0

On 24 dec, 08:08, Barbara Schwarz <BarbaraSchwarz2...@excite.com>
wrote:
>

So you admit visiting XXX-sites? Naughty girl!!

barb

12/24/2007 8:35:00 AM

0

navy.internetoplichting@gmail.com wrote:
> On 24 dec, 08:08, Barbara Schwarz <BarbaraSchwarz2...@excite.com>
> wrote:
>
> So you admit visiting XXX-sites? Naughty girl!!
>

She needs inspiration for her dildo collection, I'd suspect.

--
barb
Chaplain, ARSCCwdne

buy my book!
http://stores.lulu.com/store.php?fAcct...

read my page! (thanks, R. Hill!)
http://www.xenu-directory.net/critics/gr...

visit my store!
http://www.cafepress.com...