marcamillion
marcamillion

Reputation: 33795

How do I modify the second element in a nested array, within an array, alone? - Ruby

So say I have a 2D array like this:

list = [["auburn", "http://auburn.craigslist.org"], ["birmingham", "http://bham.craigslist.org"], ["dothan", "http://dothan.craigslist.org"]]

I would like it to look like this:

list = [["auburn", "http://auburn.craigslist.org/web/"], ["birmingham", "http://bham.craigslist.org/web/"], ["dothan", "http://dothan.craigslist.org/web/"]]

So I have just modified the second element within each nested array.

How do I do that in Ruby?

Thanks.

Upvotes: 1

Views: 258

Answers (2)

cheetah
cheetah

Reputation: 13

list.map! { |elem| [elem[0], elem[1] << '/web/'] }

Upvotes: 1

intellidiot
intellidiot

Reputation: 11228

Try this

list.map!{|f,l| [f, l + "/web/"]}

Upvotes: 3

Related Questions