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
This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.