How to convert a string to an array of chars in ruby? -
let's have string "hello"
, want array of chars in return ["h", "e", "l", "l", "o"]
.
although it's simple question couldn't find direct answer.
there several ways array out of string. #chars
shortcut thestring.each_char.to_a
direct in opinion
>> "hello".chars => ["h", "e", "l", "l", "o"]
the other ways same result "hello".split(//)
less intention-revealing.
Comments
Post a Comment