[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Strict conversion of strings to ints?

Kenneth McDonald

8/26/2007 3:19:00 AM

Is there an easy way to parse a string to an integer that will throw an
error of some sort if the string is not actually a properly formatted
integer? "to_i" of course is very non-strict, which is no good for what
I want, and I'd prefer not to write my own when it seems likely that
there would be some way to do this already...


Thanks,
Ken

7 Answers

James Gray

8/26/2007 3:26:00 AM

0

On Aug 25, 2007, at 10:19 PM, Kenneth McDonald wrote:

> Is there an easy way to parse a string to an integer that will
> throw an error of some sort if the string is not actually a
> properly formatted integer? "to_i" of course is very non-strict,
> which is no good for what I want, and I'd prefer not to write my
> own when it seems likely that there would be some way to do this
> already...

>> Integer("junk")
ArgumentError: invalid value for Integer: "junk"
from (irb):1:in `Integer'
from (irb):1

Hope that helps.

James Edward Gray II

Kenneth McDonald

8/26/2007 8:27:00 PM

0

The real problem is:

irb(main):002:0> "12,345".to_i
=> 12

Basically, Ruby converts anything that starts with an int, which is
really quite nasty in a case such as the above. I spent a lot of time
tracking down that problem.

So, I want an integer conversion function that throws an error if the
_entire_ string can't be interpreted as an integer; the Ruby analog to
the standard string-to-int conversion function in most other languages.


Thanks,
Ken

James Edward Gray II wrote:
> On Aug 25, 2007, at 10:19 PM, Kenneth McDonald wrote:
>
>> Is there an easy way to parse a string to an integer that will throw
>> an error of some sort if the string is not actually a properly
>> formatted integer? "to_i" of course is very non-strict, which is no
>> good for what I want, and I'd prefer not to write my own when it
>> seems likely that there would be some way to do this already...
>
> >> Integer("junk")
> ArgumentError: invalid value for Integer: "junk"
> from (irb):1:in `Integer'
> from (irb):1
>
> Hope that helps.
>
> James Edward Gray II
>
>


James Gray

8/26/2007 8:32:00 PM

0

On Aug 26, 2007, at 3:26 PM, Kenneth McDonald wrote:

> The real problem is:
>
> irb(main):002:0> "12,345".to_i
> => 12
>
> Basically, Ruby converts anything that starts with an int, which is
> really quite nasty in a case such as the above. I spent a lot of
> time tracking down that problem.
>
> So, I want an integer conversion function that throws an error if
> the _entire_ string can't be interpreted as an integer; the Ruby
> analog to the standard string-to-int conversion function in most
> other languages.

raise "Bad integer" unless i_str =~ /\A-?\d+\z/

James Edward Gray II

Mike Stok

8/26/2007 8:38:00 PM

0


On 26-Aug-07, at 4:32 PM, James Edward Gray II wrote:

> On Aug 26, 2007, at 3:26 PM, Kenneth McDonald wrote:
>
>> The real problem is:
>>
>> irb(main):002:0> "12,345".to_i
>> => 12
>>
>> Basically, Ruby converts anything that starts with an int, which
>> is really quite nasty in a case such as the above. I spent a lot
>> of time tracking down that problem.
>>
>> So, I want an integer conversion function that throws an error if
>> the _entire_ string can't be interpreted as an integer; the Ruby
>> analog to the standard string-to-int conversion function in most
>> other languages.
>
> raise "Bad integer" unless i_str =~ /\A-?\d+\z/

What about

int = Integer(i_str)

?

It accepts leading whitespace, but apart from that it's pretty picky.

Mike

--

Mike Stok <mike@stok.ca>
http://www.stok...

The "`Stok' disclaimers" apply.





Sebastian Hungerecker

8/26/2007 8:42:00 PM

0

Kenneth McDonald wrote:
> James Edward Gray II wrote:
> > >> Integer("junk")
> >
> > ArgumentError: invalid value for Integer: "junk"

> So, I want an integer conversion function that throws an error if the
> _entire_ string can't be interpreted as an integer; the Ruby analog to
> the standard string-to-int conversion function in most other languages.

Ehrm,
>>Integer("12,345")
ArgumentError: invalid value for Integer: "12,345"

That's exactly what you wanted, right? So what's the problem?


--
NP: Anathema - The Sweet Suffering
Jabber: sepp2k@jabber.org
ICQ: 205544826

Joel VanderWerf

8/26/2007 8:58:00 PM

0

James Edward Gray II wrote:
> raise "Bad integer" unless i_str =~ /\A-?\d+\z/

Depending on your needs (do you want to accept all the strings that ruby
accepts as integers?), you might want to add _ to that regex (also,
preceding '+' char):

irb(main):001:0> i_str = "12_345"
=> "12_345"
irb(main):002:0> raise "Bad integer" unless i_str =~ /\A-?\d+\z/
RuntimeError: Bad integer
from (irb):2
irb(main):003:0> Integer(i_str)
=> 12345

But getting that right is harder than just using Integer().

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Kenneth McDonald

8/26/2007 9:42:00 PM

0

Mike Stok wrote:
>
> On 26-Aug-07, at 4:32 PM, James Edward Gray II wrote:
>
>> On Aug 26, 2007, at 3:26 PM, Kenneth McDonald wrote:
>>
>>> The real problem is:
>>>
>>> irb(main):002:0> "12,345".to_i
>>> => 12
>>>
>>> Basically, Ruby converts anything that starts with an int, which is
>>> really quite nasty in a case such as the above. I spent a lot of
>>> time tracking down that problem.
>>>
>>> So, I want an integer conversion function that throws an error if
>>> the _entire_ string can't be interpreted as an integer; the Ruby
>>> analog to the standard string-to-int conversion function in most
>>> other languages.
>>
>> raise "Bad integer" unless i_str =~ /\A-?\d+\z/
>
> What about
>
> int = Integer(i_str)
>
> ?
>
> It accepts leading whitespace, but apart from that it's pretty picky.
>
> Mike
This is what I was looking for. There's nothing wrong with the regexp
solution of course, it's just that Simpler is Nicer :-). Many thanks to
both of you.

Cheers,
Ken
>
> --
> Mike Stok <mike@stok.ca>
> http://www.stok...
>
> The "`Stok' disclaimers" apply.
>
>
>
>
>
>