hash with files

D

Derek Smith

Hello all,

I am trying to run File.size? on each file in my hash, yet I am getting
this error.
Goal ==> get file sizes stored in hash from Dir.glob

$ ruby test.rb
yes
test.rb:49:in `size?': can't convert Array into String (TypeError)
from test.rb:49
from test.rb:48:in `each_value'
from test.rb:48


apparently my hash value has to be converted to a string b4 testing it
with File.size?

Wonder if I have to use dirhsh.inspect to convert it to a string first?

$ ruby test.rb
yes
{"vrdevgzlogs"=>["derek.log.gz"], "vrdevlogs"=>["derek.log"]}
{"vrdevgzlogs"=>["derek.log.gz"], "vrdevlogs"=>["derek.log"]}


thank you!

Attachments:
http://www.ruby-forum.com/attachment/3543/test.rb.txt
 
M

Martin DeMello

Hello all,

I am trying to run File.size? on each file in my hash, yet I am getting
this error.
Goal =3D=3D> get file sizes stored in hash from Dir.glob

$ ruby test.rb
yes
test.rb:49:in `size?': can't convert Array into String (TypeError)
=A0 =A0 =A0 =A0from test.rb:49
=A0 =A0 =A0 =A0from test.rb:48:in `each_value'
=A0 =A0 =A0 =A0from test.rb:48

Dir.glob returns an array of matches. Could you post the code where
you're constructing the hash?

martin
 
D

Derek Smith

Martin said:
Dir.glob returns an array of matches. Could you post the code where
you're constructing the hash?

martin

Hi Martin,

here it is. its also in the attachment.


Dir.chdir("/cygdrive/c/temp/log") or raise StandardError, "Change dir
failed to ~aevvrlog!"


###-- Create hash with files for rolling from dir above --###
dirhsh = Hash.new 0
dirhsh["vrdevlogs"] = Dir.glob("*.{log,out}")
dirhsh["vrdevgzlogs"] = Dir.glob("*.{log,out}.gz")

if ( dirhsh.length > 0 )
puts "yes"
dirhsh.each_value { |value|
puts dirhsh.inspect
if (File.size?(value) > FSIZE)
puts "Yes its larger"
end
else
raise StandardError, "Hash 'dirhsh' has no values, logs will not be
rolled!"
end
 
M

Martin DeMello

Hi Martin,

here it is. its also in the attachment.


Dir.chdir("/cygdrive/c/temp/log") or raise StandardError, "Change dir
failed to ~aevvrlog!"


###-- Create hash with files for rolling from dir above --###
dirhsh =A0 =A0=3D Hash.new 0
dirhsh["vrdevlogs"] =A0 =3D Dir.glob("*.{log,out}")
dirhsh["vrdevgzlogs"] =3D Dir.glob("*.{log,out}.gz")

Note that Dir.glob returns an array, since there could be several matching =
files
if ( dirhsh.length > 0 )
=A0 =A0puts "yes"
=A0 =A0dirhsh.each_value { |value|

So here value is an array of several files
=A0 =A0 =A0 =A0puts dirhsh.inspect

So now we want

value.each {|filename|
if File.size(filename) > FSIZE
# ....


martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top