# Working from a string to an array

**URL:** <https://rubytalk.org/t/working-from-a-string-to-an-array/64015>\
**Category:** ruby-talk\
**Created:** [1 November 2011 01:04 UTC](https://rubytalk.org/t/working-from-a-string-to-an-array/64015 "2011-11-01T01:04:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_A](https://avatars.discourse-cdn.com/v4/letter/p/a587f6/32.png) [@Paul\_A](https://rubytalk.org/u/Paul_A)\
**Post date:** [1 November 2011 01:04 UTC](https://rubytalk.org/t/working-from-a-string-to-an-array/64015/1 "2011-11-01T01:04:47Z")

</div>

Supposing there's the following key-value:

&nbsp;&nbsp;&nbsp;&nbsp;hash = {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@bob: 42,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@joe: 1,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@tom: 16,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@luc: 872,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@mike: 32,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;&nbsp;}

And we would like to transform those string such as:

&nbsp;&nbsp;&nbsp;&nbsp;transform("Hello @bob!")  
&nbsp;&nbsp;&nbsp;&nbsp;# =\> ["Hello ", 42, "!"]  
&nbsp;&nbsp;&nbsp;&nbsp;transform("Today @bob and @mike are coding.")  
&nbsp;&nbsp;&nbsp;&nbsp;# =\> ["Today ", 42, " and ", 32, " are coding."]  
&nbsp;&nbsp;&nbsp;&nbsp;transform("Hello world.")  
&nbsp;&nbsp;&nbsp;&nbsp;# =\> ["Hello world."]  
&nbsp;&nbsp;&nbsp;&nbsp;transform("@tom? TOMCAST! HAHAHA")  
&nbsp;&nbsp;&nbsp;&nbsp;# =\> [16, " TOMCAST! HAHAHA"]

A method such as this transform() one seems not very hard to implement.  
But according to you, want could be the more sexy way to process? With  
Ruby 1.9.2 (or 1.9.3).

Thanks.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![jake\_kaiden](https://avatars.discourse-cdn.com/v4/letter/j/a698b9/32.png) [@jake\_kaiden](https://rubytalk.org/u/jake_kaiden)\
**Post date:** [1 November 2011 18:00 UTC](https://rubytalk.org/t/working-from-a-string-to-an-array/64015/2 "2011-11-01T18:00:04Z")

</div>

hi Paul,

&nbsp;&nbsp;well, i don't know how sexy it is 😉 ...but you could write a  
`transform` method which splits the String given as its argument (using  
#split,) into an Array. you could then use #gsub! to replace any  
elements of the Array that match keys of the Hash with the value  
associated with the key...

&nbsp;&nbsp;gsub(pattern, hash) → new\_str  
&nbsp;&nbsp;[http://www.ruby-doc.org/core-1.9.2/String.html#method-i-gsub](http://www.ruby-doc.org/core-1.9.2/String.html#method-i-gsub)

&nbsp;&nbsp;hth-

&nbsp;&nbsp;-j

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [1 November 2011 20:01 UTC](https://rubytalk.org/t/working-from-a-string-to-an-array/64015/3 "2011-11-01T20:01:08Z")

</div>

Here's a version which doesn't split the string into parts:

> > &nbsp;&nbsp;&nbsp;hash = {

?\> "bob"=\> 42,  
?\> "joe"=\> 1,  
?\> "tom"=\> 16,  
?\> "luc"=\> 872,  
?\> "mike"=\> 32,  
?\> }  
=\> {"mike"=\>32, "joe"=\>1, "luc"=\>872, "tom"=\>16, "bob"=\>42}

> > &nbsp;&nbsp;&nbsp;def transform(str, hash)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str.gsub(/@(\w+)/) { hash[$1] }  
> > &nbsp;&nbsp;&nbsp;end

=\> nil

> > &nbsp;&nbsp;&nbsp;transform("Hello @bob!", hash)

=\> "Hello 42!"

But it's not much harder to meet your original requirement:

> > def transform2(str, hash)  
> > &nbsp;&nbsp;str.split(/(@\w+)/).map { |x| x =~ /^@(.+)/ ? hash[$1] : x }  
> > end

=\> nil

> > transform2("Hello @bob!", hash)

=\> ["Hello ", 42, "!"]

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Paul\_A](https://avatars.discourse-cdn.com/v4/letter/p/a587f6/32.png) [@Paul\_A](https://rubytalk.org/u/Paul_A)\
**Post date:** [1 November 2011 22:42 UTC](https://rubytalk.org/t/working-from-a-string-to-an-array/64015/4 "2011-11-01T22:42:11Z")

</div>

Brian Candler wrote in post #1029644:

> > > def transform2(str, hash)  
> > > &nbsp;&nbsp;str.split(/(@\w+)/).map { |x| x =~ /^@(.+)/ ? hash[$1] : x }  
> > > end
> 
> =\> nil
> 
> > > transform2("Hello @bob!", hash)
> 
> =\> ["Hello ", 42, "!"]

Very cool solution! I didn't thought about using $1 after split.  
Awesome.  
Thanks.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
