[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

a strict version of to_f ?

furufuru

5/5/2006 11:07:00 AM

Hello all,

I'm wondering if there's a strict version of to_f,
such as

"1.3".strict_to_f # -> 1.3
"hello".strict_to_f # raises an exception

One could implement it as

def String.strict_to_f
if self =~ regex_floating_point
return self.to_f
else
raise some_exception
end
end

But, 1) it's not trivial (to me) to construct a correct
regex_floating_point and 2) somebody may already have done
this (that is, I don't want to reinvent the wheel).

I'm my applications, raising an exception to

"hello".to_f

is *always* better than returning 0.0 .

Cheers,
Ryo

7 Answers

ts

5/5/2006 11:09:00 AM

0

>>>>> "R" == Ryo <furufuru@ccsr.u-tokyo.ac.jp> writes:

R> "1.3".strict_to_f # -> 1.3
R> "hello".strict_to_f # raises an exception

#Float

moulon% ruby -e 'Float("hello")'
-e:1:in `Float': invalid value for Float(): "hello" (ArgumentError)
from -e:1
moulon%


--

Guy Decoux

Tim Hunter

5/5/2006 11:14:00 AM

0

Ryo wrote:
> Hello all,
>
> I'm wondering if there's a strict version of to_f,
> such as
>
> "1.3".strict_to_f # -> 1.3
> "hello".strict_to_f # raises an exception
>

Try Float()

irb(main):001:0> Float("1.3")
=> 1.3
irb(main):002:0> Float("hello")
ArgumentError: invalid value for Float(): "hello"
from (irb):2:in `Float'
from (irb):2

Marcin Mielzynski

5/5/2006 11:15:00 AM

0

Ryo wrote:
> Hello all,
>
> I'm wondering if there's a strict version of to_f,
> such as
>

class String
def strict_to_f
Float(self)
end
end

lopex

furufuru

5/5/2006 11:52:00 PM

0

Thank you all who responded!

Tim Hunter wrote:
> Try Float()
>
> irb(main):001:0> Float("1.3")
> => 1.3
> irb(main):002:0> Float("hello")
> ArgumentError: invalid value for Float(): "hello"
> from (irb):2:in `Float'
> from (irb):2

Ah, is this a change introduced in Ruby 1.8 ?
In fact, I knew Float, but the old pickaxe book says

Float Float( arg ) -> aFloat
Returns arg converted to a float. Numeric types are converted
directly, nil is converted to 0.0, and the rest are converted
using arg.to_f.

Anyway, my problem is solved.

Cheers,
Ryo

George

11/15/2009 5:18:00 AM

0

Frankie Lee <leeahkwee@yahoo.com> wrote in news:f2a014a4-0773-450a-95ec-
0cd20b2141da@x25g2000prf.googlegroups.com:

> On Nov 14, 8:49?pm, Mitchell Holman <noem...@comcast.net> wrote:
>> Frankie Lee <leeahk...@yahoo.com> wrote in news:2c810338-cc53-49e7-a62b-
>> d55341073...@f1g2000prf.googlegroups.com:
>>
>>
>>
>> > There are no Atheists in my surrounding countries.
>>
>> ? ? How do you know that?
>
> ***Religions plays a very important role here.They are all over the
> places.People have to register ,as, say"Republican or Democrats".I
> guess you know what I meant.


So how do you know whether complete strangers
are atheists or not?




Masked Avenger

11/15/2009 8:29:00 AM

0


On 14-Nov-2009, Mitchell Holman <noemail@comcast.net> wrote:

> >
> > There are no Atheists in my surrounding countries.
>
>
> How do you know that?


He doesn't ....... in fact he's blatantly lying ....... I have lived and
worked in Singapore ....... the place is crawling with atheists ...... and
there are far more Buddhists, Muslims and Hindus in Singapore than there are
Christians ...... Christians are definitely in the minority there ......

Frankie is a lying Troll .... and completely delusional .......

--
MA ....Yoiks .... and away .....

Only two things are infinite, the Universe and human stupidity .............
and I'm not sure about the Universe ..........
- A. Einstein

Does Schr?dinger's cat have 18 half lives ?

Robibnikoff

11/15/2009 10:02:00 AM

0


"Frankie Lee" <leeahkwee@yahoo.com> wrote in message
news:f2a014a4-0773-450a-95ec-0cd20b2141da@x25g2000prf.googlegroups.com...
On Nov 14, 8:49 pm, Mitchell Holman <noem...@comcast.net> wrote:
> Frankie Lee <leeahk...@yahoo.com> wrote in news:2c810338-cc53-49e7-a62b-
> d55341073...@f1g2000prf.googlegroups.com:
>
>
>
> > There are no Atheists in my surrounding countries.
>
> How do you know that?

***Religions plays a very important role here.They are all over the
places.People have to register ,as, say"Republican or Democrats".I

Where do you have to register your religion, liar?
--
Robyn
Resident Witchypoo & Belly Dancer Supreme
BAAWA Knight
#1557