Forum: Ruby on Rails how to convert string into int

A50f246ffcb114ab540fc5b362cab117?d=identicon&s=25 Prashant Jadhav (prashant)
on 2007-01-03 12:15
hi all
i got a array int by str.scan() method
just tht the numbers are stored in array in for of string

and i have to add this numbers in array

so how can i convert this number strings in integers

pls tell me a method or procedure

bbye
3ab52dec8038c52a74f8bb2b462450f4?d=identicon&s=25 Chris (Guest)
on 2007-01-03 12:31
If string contains a number in String form, let's say '123', then:

Integer(string) #=> 123

-Chris
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2007-01-03 13:46
(Received via mailing list)
On Jan 3, 2007, at 3:15 AM, Prashant Jadhav wrote:

> i got a array int by str.scan() method
> just tht the numbers are stored in array in for of string
>
> and i have to add this numbers in array
>
> so how can i convert this number strings in integers

~/Desktop: irb
irb(main):005:0> "1 2 3 4 5".scan(/\d+/).inject(0) { |sum,item| sum
+item.to_i }
=> 15

--
-- Tom Mornini, CTO
-- Engine Yard, Ruby on Rails Hosting
-- Reliability, Ease of Use, Scalability
-- (866) 518-YARD (9273)
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.