scmuser created the topic: Common ways to read a file in Ruby?
[code language=”css”]
File.open(“my/file/path”, “r”) 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):
[code language=”css”]
f = File.open(“my/file/path”, “r”)
f.each_line do |line|
puts line
end
f.close
[/code]
Source – stackoverflow.com/questions/5545068/what…-read-a-file-in-ruby
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND