Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
array and data types
zabouth
7/7/2007 2:56:00 PM
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-...
.
3 Answers
dblack
7/7/2007 3:02:00 PM
0
Bojan Mihelac
7/7/2007 3:43:00 PM
0
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.m...
Pete Yandell
7/8/2007 12:16:00 PM
0
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
http://no...
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
array and data types
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password