[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

nil.to_? and ruby2.0

Gergely Kontra

5/6/2005 1:42:00 PM

Hi!

To the nil war:

Then what is the reason nil.to_i and friends exists?

Will it hide bugs?

I'm using 1.8, there nil.to_i exists,but
a=nil
b=2

b+a # nil cannot be coerced into Fixnum

So where to use nil.to_i?

ps: and how can I correctly reply to a message, if I don't get mails,
but read the mails from the archive?


Another question of mine: where can I read docs about ruby1.9/ruby2.0?
Shall I DL a snapshot and try it myself, or is there a good doc
somewhere?

thx
Gergo
--
+-[ Gergely Kontra <kgergely@mcl.hu> http://www.mcl.hu... ]------+
| PhD Student (Room:IB113) PGP ID: 0x7E3846BF Mobile:(+36 20)356 9656 |
| Budapest University of Technology and Economics ICQ# 175564914 |
+------------- "Olyan langesz vagyok, hogy poroltoval kellene jarnom!" -+


3 Answers

Austin Ziegler

5/6/2005 1:50:00 PM

0

On 5/6/05, Gergely Kontra <kgergely@mcl.hu> wrote:
> To the nil war:
>
> Then what is the reason nil.to_i and friends exists?
>
> Will it hide bugs?
>
> I'm using 1.8, there nil.to_i exists, but
> a = nil
> b = 2
> b+a # => nil cannot be coerced into Fixnum
>
> So where to use nil.to_i?

b + a.to_i # I might have String, Fixnum, Float, or even nil here...

-austin

> ps: and how can I correctly reply to a message, if I don't get
> mails, but read the mails from the archive?

Either subscribe to the list, or go through the google groups
interface for comp.lang.ruby -- or maybe the gmane interface for the
same.

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



mark sparshatt

5/6/2005 2:00:00 PM

0

Gergely Kontra wrote:
> Hi!
>
> To the nil war:
>
> Then what is the reason nil.to_i and friends exists?
>
> Will it hide bugs?
>
> I'm using 1.8, there nil.to_i exists,but
> a=nil
> b=2
>
> b+a # nil cannot be coerced into Fixnum
>
> So where to use nil.to_i?
>
you'd use nil.to_i in a case like this

a = some_method
2 + a.to_i

if you knew that some_method could return nil and that treating it as
zero was the right thing to do. I.E. if you've made a concious desicion
to treat nil as zero

However if we allowed this

a = some_method
2 + a #=>

then whether or not it's reasonable for some_method to return a nil, it
would still be silently handled whether you want it to or not.

So to_i and friends are there so a developer can use them to hide nils
if the program warrants it, but this hiding isn't the default behaviour,
so unexpected nils don't get hidden.

Which seems a reasonable compromise between flexibility and error detection.

--
Mark Sparshatt



Charles Mills

5/6/2005 2:11:00 PM

0


Gergely Kontra wrote:
> Hi!
>
> To the nil war:
>
> Then what is the reason nil.to_i and friends exists?
>
> Will it hide bugs?
>
> I'm using 1.8, there nil.to_i exists,but
> a=nil
> b=2
>
> b+a # nil cannot be coerced into Fixnum
>
> So where to use nil.to_i?
>
Perhaps your thinking of to_int - which isn't defined for nil and why
the above raises an error.

> ps: and how can I correctly reply to a message, if I don't get mails,

> but read the mails from the archive?
>
>
> Another question of mine: where can I read docs about
ruby1.9/ruby2.0?
> Shall I DL a snapshot and try it myself, or is there a good doc
> somewhere?

Seems like there are some bits and pieces scattered accross the
internet, but nothing _complete_. Maybe start here:
http://www.rubygarden.org...

-Charlie

>
> thx
> Gergo
> --
> +-[ Gergely Kontra <kgergely@mcl.hu> http://www.mcl.hu...
]------+
> | PhD Student (Room:IB113) PGP ID: 0x7E3846BF Mobile:(+36 20)356
9656 |
> | Budapest University of Technology and Economics ICQ#
175564914 |
> +------------- "Olyan langesz vagyok, hogy poroltoval kellene
jarnom!" -+