I am trying to take the contents of a file which contains the results of
a file size search and print those out with ruby. To obtain the file
size of each line in that file.
#!/usr/bin/ruby
f = File.open("/tmp/lg_files")
contentsArray=[] # start with an empty array
f.each_line {|line|
a = contentsArray.push line #b = File.size(a)
puts a
}
This results in each line in the file /tmp/lg_files printing.
I am trying to take the contents of a file which contains the results of
a file size search and print those out with ruby. To obtain the file
size of each line in that file.
#!/usr/bin/ruby
f = File.open("/tmp/lg_files")
do this in a block so it closes the file afterwards:
File.read("/tmp/lg_files") do |f|
contentsArray=[] # start with an empty array
f.each_line {|line|
a = contentsArray.push line
where did “a” come from? You already have a variable storing this
information: contentsArray. #b = File.size(a)
try “File.size contentsArray.last” instead
puts a
}
This results in each line in the file /tmp/lg_files printing.
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?
#!/usr/bin/ruby
system(‘find /var -type f -size +20000 >> /tmp/lg_files’) #do this in a block so it closes the file afterwards:
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 #system(’> /tmp/lg_files’)
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
Regardless, you don’t need that at all. Something like this:
File.open(“/tmp/lg_files”,“r”).readlines.each do |line|
line.chomp!;
puts “#{line} #{File.size(line)}”
end
On Thu, Feb 14, 2013 at 12:49 AM, Hassan S. [email protected] wrote:
}
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