Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

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

scmuser created the topic: Add each array element to the lines of a file in ruby

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

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):

[code language=”css”]
File.open(“test.txt”, “w+”) do |f|
a.each { |element| f.puts(element) }
end
[/code]

Or pass the whole array to puts:

[code language=”css”]
File.open(“test.txt”, “w+”) do |f|
f.puts(a)
end
[/code]

Source –http://stackoverflow.com/questions/18900474/add-each-array-element-to-the-lines-of-a-file-in-ruby

Rajesh Kumar
Follow me
Latest posts by Rajesh Kumar (see all)
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x