I'm getting a string of few lines from the shell. Is it possible to get an Array with each line being its element?
2 Answers
The original question was splitting on lines, and the split function, by default, splits on white space. While that may be sufficient, you may want to pass in a regular expression, as in:
`ls -l`.split(/$/)
Which returns each line in a separate element in the array. However, it doesn't get rid of the initial carriage return or line feed. For that, you will want to use the map function to iterate over the array and apply strip to each, as in:
`ls -l`.split(/$/).map(&:strip)
2 Comments
user180574
Your method is not as good as just using "split". I test with
sudo ifquery --list | grep -v lo. Yours return ["eth0", "eth1", ""], the last one is annoying.Ana María Martínez Gómez
but split doesn't work if you have '\n' in your output (as part of the name for example)