[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

nil question

Brian Blazer

1/30/2005 3:57:00 AM

I am a bit new to this, so please be gentle. I was wondering about the
rational of using 'nil' to represent not only 'nothing' but also
'false' in a conditional expression. In the very few other languages I
am familiar with, nil (null) is nothing wile 0 is the return for false.

Could this be an area that may cause some problems for those
inexperienced in ruby (like myself)?

Thanks,
Brian



17 Answers

Joao Pedrosa

1/30/2005 4:01:00 AM

0

Hi,

On Sun, 30 Jan 2005 12:57:20 +0900, Brian Blazer <brian@brianandkate.com> wrote:
> I am a bit new to this, so please be gentle. I was wondering about the
> rational of using 'nil' to represent not only 'nothing' but also
> 'false' in a conditional expression. In the very few other languages I
> am familiar with, nil (null) is nothing wile 0 is the return for false.
>
> Could this be an area that may cause some problems for those
> inexperienced in ruby (like myself)?

Maybe you will enjoy this. I like it. It's a feature for me. :-)

Cheers,
Joao


Sam Roberts

1/30/2005 4:15:00 AM

0

Quoteing brian@brianandkate.com, on Sun, Jan 30, 2005 at 12:57:20PM +0900:
> I am a bit new to this, so please be gentle. I was wondering about the
> rational of using 'nil' to represent not only 'nothing' but also
> 'false' in a conditional expression. In the very few other languages I
> am familiar with, nil (null) is nothing wile 0 is the return for false.

Could this be perl?

In C (exprs evaling to) zero is false, anything else is true, including
empty strings, "".

In perl, zero is false, but also the empty string is false, and don't
you get an error if you try to test undef?

In ruby, zero and empty strings are true, and nil kinda means what undef
does, except you can test it.

I *think* that in Pascal, only booleans (true/false) can be tested for
truth, numbers, ptrs, funs, these things are not allowed to be used
as if they were booleans. Don't quote me, I haven't done any pascal.

So, I'd say there isn't anything particularly the same about any of
those 4 languages.

> Could this be an area that may cause some problems for those
> inexperienced in ruby (like myself)?

If it was exactly like some other language (that you have
experienced), it will be unlike some other language that somebody
else has experienced.

How languages treat booleans is one of the things usually different, and
a src of much flame wars, so when moving between languages you shouldn't
assume anything, you need to ask.

Have fun,
Sam



William James

1/30/2005 5:47:00 AM

0

Sam Roberts wrote
> In ruby, zero and empty strings are true

Since 0 is true, you should be able to do this in Ruby:

puts "yes" if -5 < x < 9

The phrase '-5 < x' should yield the value of x instead of true.
That's the way it actually works in the Icon programming language.
But we have to use the klunky

puts "yes" if -5 < x and x < 9

Austin Ziegler

1/30/2005 1:27:00 PM

0

On Sun, 30 Jan 2005 14:50:49 +0900, William James <w_a_x_man@yahoo.com> wrote:
> Sam Roberts wrote
> > In ruby, zero and empty strings are true
>
> Since 0 is true, you should be able to do this in Ruby:
>
> puts "yes" if -5 < x < 9
>
> The phrase '-5 < x' should yield the value of x instead of true.
> That's the way it actually works in the Icon programming language.
> But we have to use the klunky
>
> puts "yes" if -5 < x and x < 9

Please see the thread beginning at [ruby-talk:42410]. It's also
available on Google Groups from: http://q...

This was a question I asked over two years ago, and Hugh Sasse had
suggested some time before. I'm still of the opinion that it would be
interesting, but I'm less convinced that it's necessary. See, you can
also do:

puts "yes" if x.between?(-5, 9)
puts "yes" if (-5..9).include?(x)

-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca


Martin DeMello

1/30/2005 1:47:00 PM

0

Austin Ziegler <halostatue@gmail.com> wrote:
> interesting, but I'm less convinced that it's necessary. See, you can
> also do:
>
> puts "yes" if x.between?(-5, 9)
> puts "yes" if (-5..9).include?(x)

I still like

class Object
def in?(other)
other.include?(self)
end
end

puts "yes" if x.in?(-5..9)

martin

Christian Neukirchen

1/30/2005 1:56:00 PM

0

"William James" <w_a_x_man@yahoo.com> writes:

> Sam Roberts wrote
>> In ruby, zero and empty strings are true
>
> Since 0 is true, you should be able to do this in Ruby:
>
> puts "yes" if -5 < x < 9
>
> The phrase '-5 < x' should yield the value of x instead of true.
> That's the way it actually works in the Icon programming language.
> But we have to use the klunky
>
> puts "yes" if -5 < x and x < 9
>

Erm, say, x is -16:

(-5 < x) < 9
(-5 < -16) < 9
-5 < 9
-5

-5 is true, probably not what you want.

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...


dblack

1/30/2005 2:14:00 PM

0

Christian Neukirchen

1/30/2005 3:31:00 PM

0

"David A. Black" <dblack@wobblini.net> writes:

> Hi --
>
> On Sun, 30 Jan 2005, Christian Neukirchen wrote:
>
>> "William James" <w_a_x_man@yahoo.com> writes:
>>
>>> Sam Roberts wrote
>>>> In ruby, zero and empty strings are true
>>>
>>> Since 0 is true, you should be able to do this in Ruby:
>>>
>>> puts "yes" if -5 < x < 9
>>>
>>> The phrase '-5 < x' should yield the value of x instead of true.
>>> That's the way it actually works in the Icon programming language.
>>> But we have to use the klunky
>>>
>>> puts "yes" if -5 < x and x < 9
>>>
>>
>> Erm, say, x is -16:
>>
>> (-5 < x) < 9
>> (-5 < -16) < 9
>> -5 < 9
>> -5
>>
>> -5 is true, probably not what you want.
>
> But -5 < -16 is not true, so it wouldn't get that far. (I assume
> William means it should return x if the expression is true, false
> otherwise.)

So false is bigger than 9? Math books will need to be rewritten. :-)
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...


dblack

1/30/2005 3:46:00 PM

0

Christian Neukirchen

1/30/2005 6:28:00 PM

0

"David A. Black" <dblack@wobblini.net> writes:

>> So false is bigger than 9? Math books will need to be rewritten. :-)
>
> I assume the expression would short-circuit once one of the
> sub-expressions returned false, since
>
> x < y < z
>
> cannot be true unless x < y. So there would never be a false < z
> comparison.

So < would get a special operator.

Please keep my beloved < as it is!
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...