OneBite.Dev - Coding blog in a bite size

convert a string to an array in Ruby

Code snippet on how to convert a string to an array in Ruby

  array = string.split(" ")

This code will take a string (string) and split it into an array. The split method takes a parameter - in this example, a space character is used - and creates an array from the string with each word separated into its own element. For example, the string “Hello world” would be split into ["Hello", "world"]. The result is stored in the array variable.

ruby