Ruby/Array/join

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

join all the elements together into one big string by calling the join method on the array

x = ["Word", "Play", "Fun"]
puts x.join



join method can take an optional parameter that"s placed between each element in the resulting string

x = ["Word", "Play", "Fun"]
puts x.join(", ")



use the compact method, which removes all nils from an array.

months = %w[ nil January February March April May June July August September
October November December ]
puts months.rupact.join( ", " )