Types? casting and bools

Hi!

Recently I ran into the problem:

Why ruby handle booleans so liberally?
I mean why can I pass anything to an if, and for constructs requiring
bool? I guess many programmers do it the wrong way saying “if 0” and
expecting to be true…

And on the other hand I must use to_i and others to convert from one
type to another.

Gergo

···


±[ Kontra, Gergelykgergely@mcl.hu 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 --+

Hi –

Hi!

Recently I ran into the problem:

Why ruby handle booleans so liberally?
I mean why can I pass anything to an if, and for constructs requiring
bool? I guess many programmers do it the wrong way saying “if 0” and
expecting to be true…

It is true:

if 0
puts “yes”
end

=> yes

And on the other hand I must use to_i and others to convert from one
type to another.

You mean you want to have to put “to_bool” everywhere? :slight_smile:

David

···

On Sat, 13 Dec 2003, KONTRA Gergely wrote:


David A. Black
dblack@wobblini.net

It doesn’t. Ruby treats all values excepting false and nil as true. It
sounds like you’re wanting 0 to be treated as false.

-austin

···

On Sat, 13 Dec 2003 01:56:18 +0900, KONTRA Gergely wrote:

Why ruby handle booleans so liberally?


austin ziegler * austin@halostatue.ca * Toronto, ON, Canada
software designer * pragmatic programmer * 2003.12.12
* 13.13.22

Why ruby handle booleans so liberally?
I mean why can I pass anything to an if, and for constructs requiring
bool? I guess many programmers do it the wrong way saying “if 0” and
expecting to be true…

It is true:

I meant false, of course…

And on the other hand I must use to_i and others to convert from one
type to another.
You mean you want to have to put “to_bool” everywhere? :slight_smile:

Not everywhere, just where needed…
Where should you put these? (examples)

Gergo

···

On 1213, David A. Black wrote:


±[ Kontra, Gergelykgergely@mcl.hu 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 --+

No, I’m just asking how ruby “dare” to convert my 0 to true (or false, it
is the same).
Everyone can write his/her own to_bool method INLINE. Shouldn’t be a big
deal, but it can avoid some gotchas…

So in my philosophy, if 0 should raise a typeerror

So you are forced to write if 0!=0

(of course a=0;if a → a=0; if a!=0 )

Do you think it’s the camel’s face?

Gergo

···

On 1213, Austin Ziegler wrote:

On Sat, 13 Dec 2003 01:56:18 +0900, KONTRA Gergely wrote:

Why ruby handle booleans so liberally?
It doesn’t. Ruby treats all values excepting false and nil as true. It
sounds like you’re wanting 0 to be treated as false.

±[ Kontra, Gergelykgergely@mcl.hu 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 --+

Well, there is no #to_bool method. What would 1.to_bool be, anyway? Remember
that in some cases, 0 is success or true and non-zero is failure or false.

IMO, Ruby gets past this by essentially requiring explicit boolean tests.
The equivalent idiom for if 0 “commenting” would be if false. Not that I’ve
ever seen it done in Ruby.

-austin

···

On Sat, 13 Dec 2003 20:49:18 +0900, KONTRA Gergely wrote:

On 1213, David A. Black wrote:

And on the other hand I must use to_i and others to convert from one
type to another.
You mean you want to have to put “to_bool” everywhere? :slight_smile:
Not everywhere, just where needed…
Where should you put these? (examples)


austin ziegler * austin@halostatue.ca * Toronto, ON, Canada
software designer * pragmatic programmer * 2003.12.13
* 09.59.08

Outside of any programming language, an abstract boolean has only two
values: true, and false.

Many programming languages map true to non-zero and false to zero, but
that isn’t necessary a given. Return values for commands in a shell are
the opposite, 0 is true, anything non-zero is false. Even in languages
like C, technically 0 means true and non-zero means false, but it’s
semantically exactly the opposite for 99.9% of all standard functions,
which return 0 for true, and non-zero for false.

Some languages, like Python or Perl, have a big list of what is false,
and everything else is true. Sometimes it’s hard to know what is going
to be true or false in those languages unless you are very familiar
with the nuances.

In digital logic design, often 0 means false, and 1 means true. Except
if you’re talking about shared bus architectures, then almost always 0
means true, 0 means false.

While it works for certain domains, it’s probably not a good idea in
general to assume that integers map have any intrinsic mapping to
booleans: it’s always a characteristic of the language or problem
domain in question.

In ruby, it’s very simple and straightforward. Only two things are
logically false: nil, and false. There isn’t any “conversion” going on,
it’s an intrinsic part of the language. There is no “Boolean” data type
that things are being “converted” to; everything is true, except
nil and false.

···

On Saturday 13 December 2003 8:37 am, KONTRA Gergely wrote:

On 1213, Austin Ziegler wrote:

On Sat, 13 Dec 2003 01:56:18 +0900, KONTRA Gergely wrote:

Why ruby handle booleans so liberally?

It doesn’t. Ruby treats all values excepting false and nil as true.
It sounds like you’re wanting 0 to be treated as false.

No, I’m just asking how ruby “dare” to convert my 0 to true (or
false, it is the same).


Wesley J. Landaker - wjl@icecavern.net
OpenPGP FP: 4135 2A3B 4726 ACC5 9094 0097 F0A9 8A4C 4CD6 E3D2

Content-Description: signed data

···

On 1214, Wesley J Landaker wrote:

On Saturday 13 December 2003 8:37 am, KONTRA Gergely wrote:

On 1213, Austin Ziegler wrote:

On Sat, 13 Dec 2003 01:56:18 +0900, KONTRA Gergely wrote:

Why ruby handle booleans so liberally?
It doesn’t. Ruby treats all values excepting false and nil as true.
It sounds like you’re wanting 0 to be treated as false.
No, I’m just asking how ruby “dare” to convert my 0 to true (or
false, it is the same).
In ruby, it’s very simple and straightforward. Only two things are
logically false: nil, and false. There isn’t any “conversion” going on,
it’s an intrinsic part of the language. There is no “Boolean” data type
that things are being “converted” to; everything is true, except
nil and false.
This is simple, but I think you must consider the above not to allow
everything to be boolean.
The time is NOW to change things (before 2.0)…

So in Rite will it be the same, or will the simplest class (boolean)
finally have its own class?

Gergo


±[ Kontra, Gergelykgergely@mcl.hu 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 --+

In ruby, it’s very simple and straightforward. Only two things are
logically false: nil, and false. There isn’t any “conversion” going on,
it’s an intrinsic part of the language. There is no “Boolean” data type
that things are being “converted” to; everything is true, except
nil and false.
This is simple, but I think you must consider the above not to allow
everything to be boolean. The time is NOW to change things (before 2.0)…

I disagree. When I first came to Ruby, I was bothered by the fact that there
is TrueClass, FalseClass, but no Boolean class. Once I got used to the fact
that I could do, though:

if foo.bar! then

end

I find that I don’t return “true” or “false” values often (only in methods I
mark as implicitly questioning, e.g., #exists?). Rather, I return nil or
not-nil. This allows people to use my method in a boolean test if they wish.

So in Rite will it be the same, or will the simplest class (boolean)
finally have its own class?

I don’t think that it needs its own class. After all, nil should still be
false, even if there is a boolean class.

-austin

···

On Sun, 14 Dec 2003 01:11:03 +0900, KONTRA Gergely wrote:

On 1214, Wesley J Landaker wrote:

austin ziegler * austin@halostatue.ca * Toronto, ON, Canada
software designer * pragmatic programmer * 2003.12.13
* 18.32.37