chilledheat
chilledheat

Reputation: 53

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

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

in_file = open(from_file)
indata = in_file.read

Upvotes: 4

Views: 4764

Answers (3)

dawg
dawg

Reputation: 103844

To read ONE line you can use File.readline

Given:

cat file
Line 1
Line 2
Line 3
Line 4

In Ruby:

f=File.open("/tmp/file")

p f.readline 
# "Line 1\n"

Upvotes: 0

SunnyMagadan
SunnyMagadan

Reputation: 1849

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

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

Upvotes: 10

dvxam
dvxam

Reputation: 604

open("README.md").read

For very small file, this is acceptable.

Upvotes: 1

Related Questions