I'm trying to create array from string from text file.
For example in txt file I have string.
"ABC;DEF;GHI"
I want to create array which looks like:
["ABC","DEF","GHI"]
I have tried that using method below:
File.open(file.txt).map { |line| line.split(/;/) }
but output of the above method was:
[["ABC","DEF","GHI"]]
You may notice that this is an array within an array.
What I should to do?
CodePudding user response:
Use flat_map
instead of map
Code
File.open(file.txt).flat_map{ |line| line.split(/;/) }
CodePudding user response:
You can use String.prototype.split() Method
"ABC;DEF;GHI".split(';')
=> ['ABC', 'DEF', 'GHI']