results = open('names.txt').map { |line| line.split(' ')[0]}
p results
n = "Names_In_Array.txt"
outfile = File.new(n, 'w')
outfile.puts(results)
outfile.close
I’m trying to add quote marks and a comma after each name so I have an array format (besides the brackets). At it’s current state, it saves it back into a plain string.
It sounds like you want to a take a file like:
ben john joe adam mike boband serialize that into an object structure for reading later.
If that is the case, I’d recommend you have a look at YAML. It’s built into Ruby and makes reading and writing data structures easy. For example:
You’re resulting file can be read in a subsequent program with