flanelman flanelman - 1 year ago 67
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


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
how would I make an array like:
["h", "e", "l", "l", "o"]
out of my

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 Source

Use String#chars


2.3.1 :009 > "Hello World".chars
=> ["H", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d"]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download