split a string in Ruby
Code snippet on how to split a string in Ruby
str = "hello world"
str.split(" ")
This code uses the split
method to split a string into separate words. The str
variable holds a string of "hello world"
, which we want to split. The split
method takes one argument, which is the character you want to split the string by. In this case, it’s a space, so the argument is " "
.
When the code runs, the split
method splits the string into two parts: "hello"
and "world"
. It then puts each of those in an array and returns that array. So the end result is ["hello", "world"]
.