{"id":2578,"date":"2017-12-09T07:13:24","date_gmt":"2017-12-09T07:13:24","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/?p=2578"},"modified":"2020-01-09T07:54:12","modified_gmt":"2020-01-09T07:54:12","slug":"add-each-array-element-to-the-lines-of-a-file-in-ruby","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/add-each-array-element-to-the-lines-of-a-file-in-ruby\/","title":{"rendered":"Add each array element to the lines of a file in ruby"},"content":{"rendered":"<p><strong>scmuser created the topic: Add each array element to the lines of a file in ruby<\/strong><\/p>\n<p>Add each array element to the lines of a file in ruby<\/p>\n<p>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):<\/p>\n<p>[code language=&#8221;css&#8221;]<br \/>\nFile.open(&#8220;test.txt&#8221;, &#8220;w+&#8221;) do |f|<br \/>\na.each { |element| f.puts(element) }<br \/>\nend<br \/>\n[\/code]<\/p>\n<p>Or pass the whole array to puts:<\/p>\n<p>[code language=&#8221;css&#8221;]<br \/>\nFile.open(&#8220;test.txt&#8221;, &#8220;w+&#8221;) do |f|<br \/>\nf.puts(a)<br \/>\nend<br \/>\n[\/code]<\/p>\n<p>Source &#8211;<a href=\"http:\/\/stackoverflow.com\/questions\/18900474\/add-each-array-element-to-the-lines-of-a-file-in-ruby\" target=\"_blank\" rel=\"noopener\">http:\/\/stackoverflow.com\/questions\/18900474\/add-each-array-element-to-the-lines-of-a-file-in-ruby<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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):&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[2],"tags":[178],"class_list":["post-2578","post","type-post","status-publish","format-standard","hentry","category-uncategorised","tag-ruby"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2578","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=2578"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2578\/revisions"}],"predecessor-version":[{"id":8535,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2578\/revisions\/8535"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=2578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=2578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=2578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}