Robert Klemme
8/17/2007 5:31:00 PM
On 17.08.2007 16:30, Kaldrenon wrote:
> On Aug 17, 9:47 am, "Robert Klemme" <shortcut...@googlemail.com>
> wrote:
>> Here's another solution that avoids reading the large file into mem
>> but it does not write out names ordered:
>>
>> require 'set'
>> names = Set.new(File.readlines("filenames.txt").each {|s| s.chomp!})
>>
>> File.open("allfilespath.txt") do |io|
>> io.each do |line|
>> line.chomp!
>> puts line if names.includes? File.basename(line)
>> end
>> end
>>
>> Kind regards
>>
>> robert
>
> 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.
Kind regards
robert