Under the Hood: “Slurping” and Streaming Files in Ruby

Under the Hood: “Slurping” and Streaming Files in Ruby

6 years ago
Anonymous $hM_jrxqbr-

https://medium.com/@AppSignal/under-the-hood-slurping-and-streaming-files-in-ruby-2641a5de72c6

Ruby’s File.read method reads a file and returns its full content.

irb> content = File.read("log/production.log")=> "I, [2018-06-27T16:45:02.843719 #9098] INFO -- : [86a5d18c-19dd-4cbf-9d7a-461c79e98c22] Started GET \"/articles\" for 127.0.0.1 at 2018-06-27 16:45:02 +0200\n...Internally, this opens the file, reads its content, closes the file, and returns the content as a single string. By “slurping” the file’s content at once, it’s kept in memory until it’s cleaned up by Ruby’s garbage collector.

Last Seen
27 minutes ago
Reputation
0
Spam
0.000
Last Seen
13 minutes ago
Reputation
0
Spam
0.000
Last Seen
10 minutes ago
Reputation
0
Spam
0.000
Last Seen
3 hours ago
Reputation
0
Spam
0.000
Last Seen
34 minutes ago
Reputation
0
Spam
0.000
Last Seen
about an hour ago
Reputation
0
Spam
0.000
Last Seen
2 hours ago
Reputation
0
Spam
0.000
Last Seen
about an hour ago
Reputation
0
Spam
0.000
Last Seen
about an hour ago
Reputation
0
Spam
0.000
Last Seen
about an hour ago
Reputation
0
Spam
0.000
Last Seen
3 hours ago
Reputation
0
Spam
0.000
Last Seen
a few minutes ago
Reputation
0
Spam
0.000
Last Seen
3 hours ago
Reputation
0
Spam
0.000