How to Convert a String to an Array of Characters in Ruby?

Learn how to split a string into an array of characters in Ruby

You can split a Ruby string into an array of characters by using the chars method, for example, like so:

chars_arr = "foo bar".chars

print chars_arr #=> ["f", "o", "o", " ", "b", "a", "r"]
print chars_arr.class #=> Array

Alternatively, you can also use the split method with no delimiter, for example, like so:

chars_arr = "foo bar".split("")

print chars_arr #=> ["f", "o", "o", " ", "b", "a", "r"]
print chars_arr.class #=> Array

Hope you found this post useful. It was published . Please show your love and support by sharing this post.