split a string into an array of words in ruby

To split a string into an array of words in Ruby, you can use the split method with a space as the delimiter. This will split the string at each whitespace character and return an array of the resulting words.

main.rb
string = "hello world"
words = string.split(" ")
# words => ["hello", "world"]
79 chars
4 lines

You can also split the string based on other delimiters, such as commas or periods, by passing them as arguments to the split method.

main.rb
string = "red,green,blue"
colors = string.split(",")
# colors => ["red", "green", "blue"]
90 chars
4 lines

related categories

gistlibby LogSnag