# String into array

**URL:** <https://www.ruby-forum.com/t/string-into-array/83649>\
**Category:** Ruby\
**Created:** [January 28, 2007, 11:15am UTC](https://www.ruby-forum.com/t/string-into-array/83649 "2007-01-28T11:15:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [January 28, 2007, 11:15am UTC](https://www.ruby-forum.com/t/string-into-array/83649/1 "2007-01-28T11:15:43Z")

</div>

I have the following string  
id = “(48.88689971942316, 2.3380279541015625)”  
is there any method to transform directly into an array  
[48.88689971942316, 2.3380279541015625] or should extract both parts  
and create the array (what I did… but not very DRY)

tfyl

joss

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [January 28, 2007, 11:25am UTC](https://www.ruby-forum.com/t/string-into-array/83649/2 "2007-01-28T11:25:44Z")

</div>

On 28.01.2007 11:13, Josselin wrote:

> I have the following string  
> id = “(48.88689971942316, 2.3380279541015625)”  
> is there any method to transform directly into an array  
> [48.88689971942316, 2.3380279541015625] or should extract both parts and  
> create the array (what I did… but not very DRY)

In this simple case you can do:

> > id = “(48.88689971942316, 2.3380279541015625)”  
> > =\> “(48.88689971942316, 2.3380279541015625)”
> 
> > id.scan /\d+.\d+/  
> > =\> [“48.88689971942316”, “2.3380279541015625”]
> 
> > id.scan(/\d+.\d+/).map! {|x| x.to\_f}  
> > =\> [48.8868997194232, 2.33802795410156]

It depends on the values that can occur in the string if this is  
sufficient (for example, the piece above does not take care of signs  
also does not recognize numbers without decimals). You can find plenty  
solutions for matching floating point numbers in the archives of this  
list / newsgroup.

Generally, if you get this string from some piece of external code, you  
want to parse it in some way to make sure it matches your expectations.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [January 28, 2007, 11:25am UTC](https://www.ruby-forum.com/t/string-into-array/83649/3 "2007-01-28T11:25:44Z")

</div>

On Jan 28, 2007, at 11:15 AM, Josselin wrote:

> I have the following string  
> id = “(48.88689971942316, 2.3380279541015625)”  
> is there any method to transform directly into an array  
> [48.88689971942316, 2.3380279541015625] or should extract both  
> parts and create the array (what I did… but not very DRY)
> 
> tfyl

id[1…-2].split(’,’).map {|e| e.to\_f }

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [January 28, 2007, 11:32am UTC](https://www.ruby-forum.com/t/string-into-array/83649/4 "2007-01-28T11:32:45Z")

</div>

On Sun, 2007-01-28 at 19:15 +0900, Josselin wrote:

> 

Since, I dunno abt really DRY way of doing that, I would go with this:

class String  
def to\_farray  
self.split(’,’).map {|part| part.gsub(/[()]/,"").to\_f}  
end  
end  
a = “(48.88689971942316, 2.3380279541015625)”  
a.to\_farray #=\> [48.88689971942316, 2.3380279541015625]

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [January 29, 2007, 8:00am UTC](https://www.ruby-forum.com/t/string-into-array/83649/5 "2007-01-29T08:00:54Z")

</div>

On 2007-01-28 11:20:27 +0100, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
said:

> =\> “(48.88689971942316, 2.3380279541015625)”
> 
> Generally, if you get this string from some piece of external code, you  
> want to parse it in some way to make sure it matches your expectations.
> 
> Kind regards
> 
> robert

thanks Robert, better that what I originally wrote : (suppressing  
parenthesis then split on comma)

@point = id.tr(‘()’, ’ ‘).split(’,')
