0

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?

2

1 Answer 1

1

Use flat_map instead of map

Code

File.open(file.txt).flat_map{ |line| line.split(/;/) }
Sign up to request clarification or add additional context in comments.

2 Comments

I know but I could not accept earlier because because the answer can be accepted at the earliest 10 min after asking the question
Ah Okay. I thought you might not be knowing it, sorry!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.