[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

hash with files

Derek Smith

4/2/2009 10:08:00 PM

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-...attachment/3543/t...

--
Posted via http://www.ruby-....

3 Answers

Martin DeMello

4/2/2009 10:29:00 PM

0

On Fri, Apr 3, 2009 at 3:38 AM, Derek Smith <derekbellnersmith@yahoo.com> w=
rote:
> 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

Derek Smith

4/2/2009 10:47:00 PM

0

Martin DeMello wrote:
> On Fri, Apr 3, 2009 at 3:38 AM, Derek Smith
> <derekbellnersmith@yahoo.com> wrote:
>> � � � �from test.rb:48:in `each_value'
>> � � � �from test.rb:48
>
> 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
--
Posted via http://www.ruby-....

Martin DeMello

4/2/2009 11:39:00 PM

0

On Fri, Apr 3, 2009 at 4:17 AM, Derek Smith <derekbellnersmith@yahoo.com> w=
rote:
>
> 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