# Array and data types

**URL:** <https://www.ruby-forum.com/t/array-and-data-types/108816>\
**Category:** Ruby\
**Created:** [July 7, 2007, 4:50pm UTC](https://www.ruby-forum.com/t/array-and-data-types/108816 "2007-07-07T16:50:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zabouth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/898d66/32.png) [@zabouth](https://www.ruby-forum.com/u/zabouth)\
**Post date:** [July 7, 2007, 4:50pm UTC](https://www.ruby-forum.com/t/array-and-data-types/108816/1 "2007-07-07T16:50:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zabouth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/898d66/32.png) [@zabouth](https://www.ruby-forum.com/u/zabouth)\
**Post date:** [July 7, 2007, 5:03pm UTC](https://www.ruby-forum.com/t/array-and-data-types/108816/2 "2007-07-07T17:03:48Z")

</div>

Hi –

On Sat, 7 Jul 2007, zabouth wrote:

> 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

---

<div class="post-metadata">

**Author:** ![zabouth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/898d66/32.png) [@zabouth](https://www.ruby-forum.com/u/zabouth)\
**Post date:** [July 7, 2007, 5:09pm UTC](https://www.ruby-forum.com/t/array-and-data-types/108816/3 "2007-07-07T17:09:49Z")

</div>

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

> 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

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

---

<div class="post-metadata">

**Author:** ![zabouth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/898d66/32.png) [@zabouth](https://www.ruby-forum.com/u/zabouth)\
**Post date:** [July 7, 2007, 5:43pm UTC](https://www.ruby-forum.com/t/array-and-data-types/108816/4 "2007-07-07T17:43:51Z")

</div>

zabouth wrote:

> 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

---

<div class="post-metadata">

**Author:** ![zabouth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/898d66/32.png) [@zabouth](https://www.ruby-forum.com/u/zabouth)\
**Post date:** [July 8, 2007, 2:17pm UTC](https://www.ruby-forum.com/t/array-and-data-types/108816/5 "2007-07-08T14:17:28Z")

</div>

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 Y.

> **[Pete Yandell — Principal Engineer / Architect](https://notahat.com/)**
>
> Pete Yandell is an experienced Principal Software Engineer and Architect based in Melbourne, Australia.
