Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open and read a file in one line in ruby

Tags:

ruby

How would this be written to be on a single line?

in_file = open(from_file)
indata = in_file.read
like image 202
chilledheat Avatar asked Oct 21 '25 11:10

chilledheat


2 Answers

File.read("/path/to/file")

It will read whole file content and return it as a result.

like image 69
SunnyMagadan Avatar answered Oct 23 '25 00:10

SunnyMagadan


open("README.md").read

For very small file, this is acceptable.

like image 29
dvxam Avatar answered Oct 23 '25 01:10

dvxam