Split string to an array
One common task that you probably want to learn how to do is spliting a string into an array (like explode function in PHP). You can do this by calling “split” method. By default it will split a string on whitespace, but you can pass it a delimiter. Delimiter can be a string or regular expression.
1 2 3 4 5 6 7 | animals = 'dog wolf cat lynx tiger' animals.split # => ["dog", "wolf", "cat", "lynx", "tiger"] "6534-234-23-45".split "-" # => ["6534", "234", "23", "45"] calc = "45 + 36 - 25 * 14" calc.split /\s*[-+*]\s*/ # => ["45", "36", "25", "14"] |








