tags:

views:

41

answers:

2

Okay, this code gives me exactly what I want but it seems that it could be cleaner, so here is the code:

colour = ["red", "white", "orange", "black"]  
x=[]  
colour.each_with_index do |c, i|  
   x[i] = "<a href='http://#{c}.test.com'&gt;#{c}&lt;/a&gt;"  
end  
tags2 = x.join(", ")  
p "The code ==>#{tags2}<=== " 

Any takers?

+4  A: 
tags2 = colour.map {|c| "<a href='http://#{c}.test.com'&gt;#{c}&lt;/a&gt;" }.join(", ")

map just calls a block for every element in the array, then returns the result array.

Matthew Flaschen
thanks, very nice - i knew there was something better, i am trying to overcome my old style coding!
rtfminc
A: 
tags = ["red", "white", "orange", "black"].map do |color|
    "<a href='http://#{color}.test.com'&gt;#{color}&lt;/a&gt;"
end.join(", ")
p "The code ==>#{tags}<==="
Flavius Stef
thanks, the map command is now on my radar.
rtfminc