Array and data types

i have looked at all the ruby manual but i cant find no way of doing
this I am very new to ruby
so here is my problem
ok first here is my code

sum = "5:5:5"
sum = sum.split(":")
sum = sum.inject {|sum, x| sum + x }
puts sum

problem is that when you use split it splits the values into strings
what i need to know is how do i change all the values in the array sum
eg
"5","5","5"
to floating point numbers
i just cant work it out any help would be much appreciated

···

--
Posted via http://www.ruby-forum.com/.

Hi --

···

On Sat, 7 Jul 2007, zabouth wrote:

i have looked at all the ruby manual but i cant find no way of doing
this I am very new to ruby
so here is my problem
ok first here is my code

sum = "5:5:5"
sum = sum.split(":")
sum = sum.inject {|sum, x| sum + x }
puts sum

problem is that when you use split it splits the values into strings
what i need to know is how do i change all the values in the array sum
eg
"5","5","5"
to floating point numbers
i just cant work it out any help would be much appreciated

Use to #to_f method:

   sum.split(":").inject(0) {|acc, x| acc + x.to_f }

David

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242\)
   RUBY FOR RAILS (http://www.manning.com/black\)
* Ruby/Rails training
     & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

Well first your fix:

puts "5:5:5".split( ":" ).inject( 0.0 ) { | sum, x | sum + x.to_f }

If this is something that you plan on using all the time you can define a new method on the String class like so:

class String
   def delimited_sum( delimiter = ' ' ) # Default delimiter is a space
     self.split( delimiter ).inject( 0.0 ) { | sum, x | sum + x.to_f }
   end
end

"5:5:5".delimited_sum( ':' )
=> 15

···

On Jul 07, 2007, at 10:56 , zabouth wrote:

i have looked at all the ruby manual but i cant find no way of doing
this I am very new to ruby
so here is my problem
ok first here is my code

sum = "5:5:5"
sum = sum.split(":")
sum = sum.inject {|sum, x| sum + x }
puts sum

problem is that when you use split it splits the values into strings
what i need to know is how do i change all the values in the array sum
eg
"5","5","5"
to floating point numbers
i just cant work it out any help would be much appreciated

--
Wayne E. Seguin
Sr. Systems Architect & Systems Admin
wayneseguin@gmail.com

zabouth wrote:

i have looked at all the ruby manual but i cant find no way of doing
this I am very new to ruby
so here is my problem
ok first here is my code

sum = "5:5:5"
sum = sum.split(":")
sum = sum.inject {|sum, x| sum + x }
puts sum

problem is that when you use split it splits the values into strings
what i need to know is how do i change all the values in the array sum
eg
"5","5","5"
to floating point numbers
i just cant work it out any help would be much appreciated

sum = sum.split(":").map {|e| e.to_f}

would work.

best,
Bojan

···

--
Bojan Mihelac
-> Ruby on Rails and Web Developer Blog : http://source.mihelac.org

zabouth wrote:

i have looked at all the ruby manual but i cant find no way of doing
this I am very new to ruby
so here is my problem
ok first here is my code

sum = "5:5:5"
sum = sum.split(":")
sum = sum.inject {|sum, x| sum + x }
puts sum

If you happen to have Rails (or just ActiveSupport) installed:

     require 'rubygems'
     require 'active_support'

     '5:5:5'.split(':').map(&:to_f).sum
     => 15.0

Pete Yandell