Forum: Ruby hash with files

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Derek S. (Guest)
on 2009-04-03 02:08
Attachment: test.rb.txt (0 Bytes)
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!
Martin DeMello (Guest)
on 2009-04-03 02:30
(Received via mailing list)
On Fri, Apr 3, 2009 at 3:38 AM, Derek S.
<removed_email_address@domain.invalid> 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
Derek S. (Guest)
on 2009-04-03 02:47
Martin DeMello wrote:
> On Fri, Apr 3, 2009 at 3:38 AM, Derek S.
> <removed_email_address@domain.invalid> 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
Martin DeMello (Guest)
on 2009-04-03 03:39
(Received via mailing list)
On Fri, Apr 3, 2009 at 4:17 AM, Derek S.
<removed_email_address@domain.invalid> wrote:
> ###-- 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")

Note that Dir.glob returns an array, since there could be several
matching files

> if ( dirhsh.length > 0 )
>    puts "yes"
>    dirhsh.each_value { |value|

So here value is an array of several files

>        puts dirhsh.inspect

So now we want

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


martin
This topic is locked and can not be replied to.