Let's say that you have a string "Hello"
and you want an array of chars in return ["H", "e", "l", "l", "o"]
.
Although it's a simple question I couldn't find a direct answer.
There are several ways to get an array out of a String. #chars
which is a shortcut for thestring.each_char.to_a
is the most direct in my opinion
>> "Hello".chars
=> ["H", "e", "l", "l", "o"]
The are other ways to get the same result like "Hello".split(//)
but they are less intention-revealing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With