Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby array of strings: split into smallest pieces

Tags:

ruby

I would like to know if there is a method in Ruby that splits an Array of String in smallest pieces. Consider:

['Cheese crayon', 'horse', 'elephant a b c']

Is there a method that turns this into:

['Cheese', 'crayon', 'horse', 'elephant', 'a', 'b', 'c']

like image 254
Sung Cho Avatar asked Dec 07 '25 02:12

Sung Cho


1 Answers

p ['Cheese crayon', 'horse', 'elephant a b c'].flat_map(&:split)
# => ["Cheese", "crayon", "horse", "elephant", "a", "b", "c"]
like image 134
steenslag Avatar answered Dec 08 '25 15:12

steenslag