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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
hi, irb(main):001:0> str="1500,my sql,date 2500,my sql,date" => "1500,my sql,date 2500,my sql,date" irb(main):002:0> hi=str.scan(/^(\d+)/) => [["1500"]] irb(main):005:0> hi[0] => ["1500"] irb(main):006:0> hi[0].to_i NoMethodError: undefined method `to_i'' for ["1500"]:Array from (irb):6 irb(main):007:0> hi[0][0].to_i => 1500 irb(main):008:0> the problem is you are getting arrays of arrays i just modified some part of your code 1)hi=str.scan(/(\d+)/) 2)hi.inject(0){|sum, element|(sum.to_i)+(element[0].to_i)} i also did irb(main):016:0> hi=str.scan(/(\d+)/).flatten => ["1500", "234", "2500"] irb(main):017:0> hi.inject(0){|sum, element|(sum.to_i)+(element.to_i)} #your code => 4234 i hope this helps regards gaurav --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 arrayThe 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---