# Convert string array to interger array in in one line

**URL:** <https://www.ruby-forum.com/t/convert-string-array-to-interger-array-in-in-one-line/152600>\
**Category:** Ruby\
**Created:** [November 5, 2008, 2:10pm UTC](https://www.ruby-forum.com/t/convert-string-array-to-interger-array-in-in-one-line/152600 "2008-11-05T14:10:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jags](https://www.ruby-forum.com/u/jags)\
**Post date:** [November 5, 2008, 2:10pm UTC](https://www.ruby-forum.com/t/convert-string-array-to-interger-array-in-in-one-line/152600/1 "2008-11-05T14:10:27Z")

</div>

hi guys

i have a array like this for e.g

[[“0”, “0”, “0”, “0”, “0”, “3”, “3”],  
[“0”, “0”, “0”, “0”, “0”, “3”, “3”],  
[“0”, “0”, “0”, “0”, “0”, “3”, “3”],  
[“0”, “3”, “0”, “0”, “0”, “0”, “3”],  
[“0”, “0”, “0”, “0”, “0”, “0”, “3”]]

how wud i convert to this

[[0, 0, 0, 0, 0, 3, 3],  
[0, 0, 0, 0, 0, 3, 3],  
[0, 0, 0, 0, 0, 3, 3],  
[0, 0, 0, 0, 0, 3, 3],  
[0, 0, 0, 0, 0, 0, 3],  
[0, 0, 0, 0, 0, 0, 3],  
[0, 3, 0, 0, 0, 0, 3],  
[0, 0, 0, 0, 0, 0, 3],

i.e a string array to integer array in 1 line if possible

pls help

---

<div class="post-metadata">

**Author:** ![jags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jags](https://www.ruby-forum.com/u/jags)\
**Post date:** [November 5, 2008, 2:16pm UTC](https://www.ruby-forum.com/t/convert-string-array-to-interger-array-in-in-one-line/152600/2 "2008-11-05T14:16:58Z")

</div>

Alle Wednesday 05 November 2008, Jags R. ha scritto:

> how wud i convert to this  
> i.e a string array to integer array in 1 line if possible
> 
> pls help

a.map{|i| i.map{|s| s.to\_i}}

Stefano

---

<div class="post-metadata">

**Author:** ![jags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jags](https://www.ruby-forum.com/u/jags)\
**Post date:** [November 5, 2008, 10:45pm UTC](https://www.ruby-forum.com/t/convert-string-array-to-interger-array-in-in-one-line/152600/3 "2008-11-05T22:45:33Z")

</div>

On Nov 5, 8:08 am, Jags R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> how wud i convert to this  
> i.e a string array to integer array in 1 line if possible
> 
> ## pls help
> 
> Posted viahttp://www.ruby-forum.com/.

Maybe this is a little clearer:

aStrings = [[“0”, “0”, “0”, “0”, “0”, “3”, “3”],  
[“0”, “0”, “0”, “0”, “0”, “3”, “3”],  
[“0”, “0”, “0”, “0”, “0”, “3”, “3”],  
[“0”, “3”, “0”, “0”, “0”, “0”, “3”],  
[“0”, “0”, “0”, “0”, “0”, “0”, “3”]]

aFixnums = aStrings.map{|i| i.map{|s| s.to\_i}}  
puts aStrings[0][0].class.to\_s # =\> String  
puts aFixnums[0][0].class.to\_s # =\> Fixnum  
puts aFixnums[0][0].integer? # =\> true (So a Fixnum is\_a Integer)

Note that the last line asserts that a Fixnum object is indeed an  
Integer

HTH,  
Richard

---

<div class="post-metadata">

**Author:** ![jags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jags](https://www.ruby-forum.com/u/jags)\
**Post date:** [November 5, 2008, 11:49pm UTC](https://www.ruby-forum.com/t/convert-string-array-to-interger-array-in-in-one-line/152600/4 "2008-11-05T23:49:24Z")

</div>

On Wed, Nov 5, 2008 at 7:08 AM, Jags R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> how wud i convert to this  
> i.e a string array to integer array in 1 line if possible  
> Here’s a weird one for your enjoyment (arr is your array)…

require ‘matrix’; p Matrix[\*arr].map {|i| i.to\_i}.to\_a

…of course, just like Stefano’s solution, the object must respond to  
the #to\_i method.

You can #join it and split it up (#each\_slice) again (my first cool  
solution, but looks _really_ ugly). Mapping twice is the easiest and  
is the way you probably should go.

Todd
