# Hash with files

**URL:** <https://www.ruby-forum.com/t/hash-with-files/164603>\
**Category:** Ruby\
**Created:** [April 3, 2009, 12:08am UTC](https://www.ruby-forum.com/t/hash-with-files/164603 "2009-04-03T00:08:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bloodniece](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bloodniece](https://www.ruby-forum.com/u/bloodniece)\
**Post date:** [April 3, 2009, 12:08am UTC](https://www.ruby-forum.com/t/hash-with-files/164603/1 "2009-04-03T00:08:28Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![bloodniece](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bloodniece](https://www.ruby-forum.com/u/bloodniece)\
**Post date:** [April 3, 2009, 12:30am UTC](https://www.ruby-forum.com/t/hash-with-files/164603/2 "2009-04-03T00:30:16Z")

</div>

On Fri, Apr 3, 2009 at 3:38 AM, Derek S.  
[removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![bloodniece](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bloodniece](https://www.ruby-forum.com/u/bloodniece)\
**Post date:** [April 3, 2009, 12:47am UTC](https://www.ruby-forum.com/t/hash-with-files/164603/3 "2009-04-03T00:47:26Z")

</div>

Martin DeMello wrote:

> On Fri, Apr 3, 2009 at 3:38 AM, Derek S.  
> [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![bloodniece](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bloodniece](https://www.ruby-forum.com/u/bloodniece)\
**Post date:** [April 3, 2009, 1:39am UTC](https://www.ruby-forum.com/t/hash-with-files/164603/4 "2009-04-03T01:39:50Z")

</div>

On Fri, Apr 3, 2009 at 4:17 AM, Derek S.  
[removed\_email\_address@domain.invalid](mailto: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
