Forum: Ruby on Rails cant covert array of string in integers

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-05 09:22
hi all,

 i took the desired numbers from the string in datatbase collum
(str=1500,my sql,date 2500,my sql,date)
 by str.scan method i got the numbers in a string array(eg="1500"
"2500")

now in the controllere method when i do the addition by of this array
elemts by method

hi=str.scan(/^(\d+)/)
   num = hi.inject(0){|sum, element|(sum.to_i)+(element.to_i)}

it gives error
'undefine method to_i'

plssssss help

also can ny1 tell me any other alternative to the abov methode to add
all the elements of the array
72ea925c0ca3d19fdd2f12fa76681624?d=identicon&s=25 Stephan Wehner (stephanwehner)
on 2007-01-06 01:50
Prashant Jadhav wrote:
> hi all,
>
>  i took the desired numbers from the string in datatbase collum
> (str=1500,my sql,date 2500,my sql,date)
>  by str.scan method i got the numbers in a string array(eg="1500"
> "2500")
>
> now in the controllere method when i do the addition by of this array
> elemts by method
>
> hi=str.scan(/^(\d+)/)
>    num = hi.inject(0){|sum, element|(sum.to_i)+(element.to_i)}
>
> it gives error
> 'undefine method to_i'
>
> plssssss help
>
> also can ny1 tell me any other alternative to the abov methode to add
> all the elements of the array

The elements are actually arrays, when I attempt to reconstruct this.

Try this

   num = hi.inject(0) { | sum, element| sum + element[0].to_i }

or take a look at hi.inspect

Stephan
This topic is locked and can not be replied to.