flanelman flanelman - 24 days ago 5
Ruby Question

Split string variable into an array

just wondering how I would split a string variable up letter by letter and make it an array

my

word
variable is a randomly selected word from an array of words:

word = $file_Arr[rand($file_Arr.length)]


how would I then split this word into individual letters and add them to an array?

example: if word pulls the word "hello" from
$file_Arr
how would I make an array like:
["h", "e", "l", "l", "o"]
out of my
word
variable

All I've been able to find online is people doing it with strings they type in and splitting on a comma, but how would I do it from a variable?

Answer

Use String#chars

http://ruby-doc.org/core-2.0.0/String.html#method-i-chars

2.3.1 :009 > "Hello World".chars
=> ["H", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d"]