Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add each array element to the lines of a file in ruby

If I have an array of strings e.g.

a = ['a', 'b', 'c', 'd']

and I want to output the elements, to a file (e.g. .txt) one per line. So far I have:

File.new("test.txt", "w+")
File.open("test.txt", "w+") do |i|
    i.write(a)
end

This gives me the array on one line of the test.txt file. How can I iterate over the array, adding each value to a new line of the file?

like image 900
edc505 Avatar asked Sep 19 '13 16:09

edc505


People also ask

How do you add elements to an array in Ruby?

unshift will add a new item to the beginning of an array. With insert you can add a new element to an array at any position.

How do you add multiple values to an array in Ruby?

Appending or pushing arrays, elements, or objects to an array is easy. This can be done using the << operator, which pushes elements or objects to the end of the array you want to append to. The magic of using << is that it can be chained.

How do I load an array into a file?

Overview. Loading an array from a text file requires several steps, including: opening the file, reading the records, parsing (splitting) the records into fields, adding the fields to an array, and closing the file. The file may be read all at once and then parsed, or processed line by line.

How do you add an array to an array in Ruby?

When coding, there are times when you might want to join arrays to make them one. In this case, you can use the concat() method in Ruby. concat() is used to join, combine, concatenate, or append arrays. The concat() method returns a new array with all of the elements of the arrays combined into one.


5 Answers

Either use Array#each to iterate over your array and call IO#puts to write each element to the file (puts adds a record separator, typically a newline character):

File.open("test.txt", "w+") do |f|
  a.each { |element| f.puts(element) }
end

Or pass the whole array to puts:

File.open("test.txt", "w+") do |f|
  f.puts(a)
end

From the documentation:

If called with an array argument, writes each element on a new line.

like image 78
Stefan Avatar answered Sep 30 '22 15:09

Stefan


There is a quite simpler solution :

IO.write("file_name.txt", your_array.join("\n"))
like image 39
Josip Đuraković Avatar answered Sep 30 '22 16:09

Josip Đuraković


As an alternate, you could simply join the array with "\n" so that each element is on a new line, like this:

a = %w(a b c d)

File.open('test.txt', 'w') {|f| f.write a.join("\n")}

If you don't want to override the values already in the text file so that you're simply adding new information to the bottom, you can do this:

a = %w(a b c d)

File.open('test.txt', 'a') {|f| f << "\n#{a.join("\n")}"}
like image 40
tigeravatar Avatar answered Sep 30 '22 15:09

tigeravatar


Use Array#each to iterate each element. When writing to the file, make sure you append newline(\n), or you will get a file with abcd as content:

a = ['a', 'b', 'c', 'd']
File.open('test.txt', 'w') do |f|
  a.each do |ch|
    f.write("#{ch}\n")
  end
end
like image 32
falsetru Avatar answered Sep 30 '22 17:09

falsetru


Another simple solution:

directory = "#{Rails.root}/public/your_directory" #create your_directory before
file_name = "your_file.txt"
path = File.join(directory, file_name)
File.open(path, "wb") { |f| f.write(your_array.join("\n")) }
like image 24
andriy Avatar answered Sep 30 '22 15:09

andriy