Split string by whitespace in Ruby

August 22, 2022  ‐ 1 min read

To split a string in Ruby on a whitespace character we can use the #split method. Which may take a pattern to split on as the first parameter, but splits the string on whitespace by default if no parameter is given.

puts "Foo Bar".split
#=> ["Foo", "Bar"]

The result of this operation is an array containing the parts that were delimited by a whitespace character.