Forum: Ruby on Rails how to convert string into int

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
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)
This topic is locked and can not be replied to.