On Aug 17, 1:30 pm, Robert Klemme <shortcut... / googlemail.com> wrote:
> > Is there a reason to do it this way instead of the slightly more
> > compact way below?
>
> > File.open("allfilespath.txt").each_line do |line|
> >   line.chomp!
> >   puts line if names.includes? File.basename(line)
> > end
>
> > Is there a difference in behavior, or only in appearance?
>
> The more compact version does not properly close the file.  Although you
> can fix that by doing
>
> File.open("allfilespath.txt").each_line do |line|
>    line.chomp!
>    puts line if names.includes? File.basename(line)
> end.close
>      ^^^^^
>
> there is still a difference: my version will close the file regardless
> how the block is left (i.e. even in case of an exception) while the
> fixed compact version does not close the file if the block is left via
> an exception.

That's good to know, glad I asked. Thanks!