On Thu, Feb 14, 2013 at 12:49 AM, Hassan Schroeder
<[email protected]> wrote:
> On Wed, Feb 13, 2013 at 3:08 PM, Sloan Ruby <[email protected]> wrote:
>> This is what I have now. I am not able to print out each line of the
>> file /tmp/lg_files. There is no output. What am i missing?
>
>> File.read("/tmp/lg_files") do |f|
>> contentsArray=[] # start with an empty array
>> f.each_line {|line|
>> #a = contentsArray.push line
>> b = File.size contentsArray.last
>> puts b
>> }
>> end
>
> You've commented out the contentsArray.push line which insures
> that it's always empty, for one thing :-)
Even worse: File.read will ignore the block. It's simply never
executed. Otherwise one would see an error.
> Regardless, you don't need that at all. Something like this:
>
> File.open("/tmp/lg_files","r").readlines.each do |line|
Better use
File.foreach "/tmp/lg_files" do |line|
That avoids reading the whole file into memory.
> line.chomp!;
> puts "#{line} #{File.size(line)}"
> end
> should do what you want with fewer moving parts.
I think that approach is still more complicated than necessary. First
of all, the temporary file can be omitted because with IO.popen and
similar methods one can directly read the output of the find command.
It gets even better: one can also do the file system search in Ruby.
There is no point in doing that externally. There are actually two
ways:
Find.find()
Pathname#find()
Example:
require 'pathname'
Pathname('/var').find do |file|
printf "%10d %s\n", file.size, file if file.file? && file.size >= 20_000
end
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
--
[email protected] |
https://groups.google.com/d/forum/ruby-talk-google?hl=en
---
You received this message because you are subscribed to the Google Groups
"ruby-talk-google" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.