Reputation: 3341
I have an array
books = ["Title 1", "Title 2", "Title 3"]
I need to iterate through this array and get a variable like this:
@books_read = "Title 1 \n Title 2 \n Title 3"
I tried this bit of code:
books.each do |book|
@books_read += "#{book} \n"
end
puts @books_read
But, the + operator does not concatenate the strings. Any leads on this please.
Cheers!
Upvotes: 3
Views: 335
Reputation: 13719
You can use Array#join: books.join(" \n ")
.
join(sep=$,) → str
Returns a string created by converting each element of the array to a string, separated by sep.
Upvotes: 4