{"id":2571,"date":"2017-12-09T06:53:56","date_gmt":"2017-12-09T06:53:56","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/?p=2571"},"modified":"2020-01-09T07:56:00","modified_gmt":"2020-01-09T07:56:00","slug":"common-ways-to-read-a-file-in-ruby","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/common-ways-to-read-a-file-in-ruby\/","title":{"rendered":"Common ways to read a file in Ruby?"},"content":{"rendered":"<p><strong>scmuser created the topic: Common ways to read a file in Ruby?<\/strong><\/p>\n<p>[code language=&#8221;css&#8221;]<br \/>\nFile.open(&#8220;my\/file\/path&#8221;, &#8220;r&#8221;) do |f|<br \/>\nf.each_line do |line|<br \/>\nputs line<br \/>\nend<br \/>\nend<br \/>\n# File is closed automatically at end of block<br \/>\n[\/code]<\/p>\n<p>It is also possible to explicitly close file after as above (pass a block to open closes it for you):<\/p>\n<p>[code language=&#8221;css&#8221;]<br \/>\nf = File.open(&#8220;my\/file\/path&#8221;, &#8220;r&#8221;)<br \/>\nf.each_line do |line|<br \/>\nputs line<br \/>\nend<br \/>\nf.close<br \/>\n[\/code]<\/p>\n<p>Source &#8211; <a href=\"http:\/\/stackoverflow.com\/questions\/5545068\/what...-read-a-file-in-ruby\" target=\"_blank\" rel=\"noopener\">stackoverflow.com\/questions\/5545068\/what&#8230;-read-a-file-in-ruby<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>scmuser created the topic: Common ways to read a file in Ruby? [code language=&#8221;css&#8221;] File.open(&#8220;my\/file\/path&#8221;, &#8220;r&#8221;) do |f| f.each_line do |line| puts line end end # File is closed automatically at end of block [\/code] It is also possible to explicitly close file after as above (pass a block to open closes it for you):&#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-2571","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\/2571","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=2571"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2571\/revisions"}],"predecessor-version":[{"id":8538,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2571\/revisions\/8538"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=2571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=2571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=2571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}