# String to Array?

**URL:** <https://www.ruby-forum.com/t/string-to-array/54287>\
**Category:** Rails\
**Created:** [February 28, 2006, 6:02pm UTC](https://www.ruby-forum.com/t/string-to-array/54287 "2006-02-28T18:02:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_Lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc348/32.png) [@Matt\_Lee](https://www.ruby-forum.com/u/Matt_Lee)\
**Post date:** [February 28, 2006, 6:02pm UTC](https://www.ruby-forum.com/t/string-to-array/54287/1 "2006-02-28T18:02:45Z")

</div>

I’ve got a string of keywords “internet explorer microsoft windows  
browser” - I want to output each word with a link\_to a controller I’ve  
built that searches based on a keyword.

I managed to split the string, but I don’t know how then to loop over it  
to output the link\_tos?

matt

–  
Matt Lee  
Senior Technical Developer  
NHS Connecting for Health

* * *

* * *

This e-mail is confidential and privileged. If you are not the intended  
recipient please accept our apologies; please do not disclose, copy or  
distribute information in this e-mail or take any action in reliance on  
its  
contents: to do so is strictly prohibited and may be unlawful. Please  
inform us that this message has gone astray before deleting it. Thank  
you  
for your co-operation.

* * *

---

<div class="post-metadata">

**Author:** ![Matt\_Lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc348/32.png) [@Matt\_Lee](https://www.ruby-forum.com/u/Matt_Lee)\
**Post date:** [February 28, 2006, 6:08pm UTC](https://www.ruby-forum.com/t/string-to-array/54287/2 "2006-02-28T18:08:54Z")

</div>

Matt Lee wrote:

> I’ve got a string of keywords “internet explorer microsoft windows  
> browser” - I want to output each word with a link\_to a controller I’ve  
> built that searches based on a keyword.
> 
> I managed to split the string, but I don’t know how then to loop over it  
> to output the link\_tos?

str.split(’ ‘).collect{|s| link\_to(s, foo)}.join(’  
’)

The collect (and inject) methods are most useful for this sort of thing.

---

<div class="post-metadata">

**Author:** ![Matt\_Lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc348/32.png) [@Matt\_Lee](https://www.ruby-forum.com/u/Matt_Lee)\
**Post date:** [February 28, 2006, 6:15pm UTC](https://www.ruby-forum.com/t/string-to-array/54287/3 "2006-02-28T18:15:08Z")

</div>

\<% input = “internet explorer microsoft windows browser” %\>  
\<%= input.split.each {|word| link\_to word, :controller=\>‘controller’,  
:action=\>‘action’} %\>

This should work, but hasn’t been tested

\_Kevin

---

<div class="post-metadata">

**Author:** ![Matt\_Lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc348/32.png) [@Matt\_Lee](https://www.ruby-forum.com/u/Matt_Lee)\
**Post date:** [February 28, 2006, 6:54pm UTC](https://www.ruby-forum.com/t/string-to-array/54287/4 "2006-02-28T18:54:50Z")

</div>

Creating an array out of a bunch of words like that is often done like:

%w{internet explorer microsoft windows browser}

You can assign that to a variable or just build off it:

words = %w{internet explorer microsoft windows browser}  
links = words.collect { |w| link\_to … }

or

%w{internet explorer microsoft windows browser}.collect …

Then you could either do the .join(’  
’) Alex mentioned or something  
like:

\<% links.each do |link| -%\> \<%= "- #{link}
" %\> \<% end -%\>

Or whatever your needs might be.

This would be where David Black’s new book, Ruby for Rails would come  
in handy, or, of course, The Pickaxe.

-Tom
