Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are important differences beetween those 3 choices.</p> <h1><code>File.open("file").each_line { |line| puts line }</code></h1> <ul> <li><code>File.open</code> opens a local file and returns a file object</li> <li>the file stays open until you call <code>IO#close</code> on it</li> </ul> <h1><code>open("file").each_line { |line| puts line }</code></h1> <p><code>Kernel.open</code> looks at the string to decide what to do with it.</p> <pre><code>open(".irbrc").class # =&gt; File open("http://google.com/").class # =&gt; StringIO File.open("http://google.com/") # =&gt; Errno::ENOENT: No such file or directory - http://google.com/ </code></pre> <p>In the second case the <code>StringIO</code> object returned by <code>Kernel#open</code> actually holds the content of <a href="http://google.com/" rel="noreferrer">http://google.com/</a>. If <code>Kernel#open</code> returns a <code>File</code> object, it stays open untill you call <code>IO#close</code> on it.</p> <h1><code>IO.foreach("file") { |line| puts line }</code></h1> <ul> <li><code>IO.foreach</code> opens a file, calls the given block for each line it reads, and closes the file afterwards.</li> <li>You don't have to worry about closing the file.</li> </ul> <h1><code>File.read("file").each { |line| puts line }</code></h1> <p>You didn't mention this choice, but this is the one I would use in most cases.</p> <ul> <li><code>File.read</code> reads a file completely and returns it as a string.</li> <li>You don't have to worry about closing the file.</li> <li>In comparison to <code>IO.foreach</code> this makes it clear, that you are dealing with a file.</li> <li>The memory complexity for this is O(n). If you know you are dealing with a small file, this is no drawback. But if it can be a big file and you know your memory complexity can be smaller than O(n), don't use this choice.</li> </ul> <p>It fails in this situation:</p> <pre><code> s= File.read("/dev/zero") # =&gt; never terminates s.each … </code></pre> <h1>ri</h1> <p>ri is a tool which shows you the ruby documentation. You use it like this on your shell.</p> <pre><code>ri File.open ri open ri IO.foreach ri File#each_line </code></pre> <p>With this you can find almost everything I wrote here and much more.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload