# Needing a little help with a ruby program

**URL:** <https://www.ruby-forum.com/t/needing-a-little-help-with-a-ruby-program/150707>\
**Category:** Ruby\
**Created:** [October 11, 2008, 9:05pm UTC](https://www.ruby-forum.com/t/needing-a-little-help-with-a-ruby-program/150707 "2008-10-11T21:05:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![spudicus87](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@spudicus87](https://www.ruby-forum.com/u/spudicus87)\
**Post date:** [October 11, 2008, 9:05pm UTC](https://www.ruby-forum.com/t/needing-a-little-help-with-a-ruby-program/150707/1 "2008-10-11T21:05:16Z")

</div>

Ok, so this is what I need to do for the program. I am not the best  
programmer in the world and I need a little help.

Assignment:

Create a program that obtains disk usage information from du and uses  
it to print a histogram of the disk usage of the directories given as  
command line arguments. The histogram should consist of a string with  
one hash mark per megabyte of disk usage.

This is what I have so far.

require ‘fileutils’

specified\_directory = Dir.new("#{ARGV[0]}")

disk\_usage\_info = `du #{specified_directory} -a`

file\_size = Hash.new(0)

disk\_usage\_file.each\_line do |file, size|  
#Builds the hash with the filename and the sizes

end

file\_size.each do|file, size|  
#changes value to histogram of numbers and shows results

```
   size = '#' * size / 1024

   if size < 0
     puts file +"\t | " + size

   else
     puts file

 end

```

end

I think I have the first part and the last part down pretty well, I am  
stuck on how to get the du output into the hash so I can output it  
correctly. Any help would be appreciated.

Thanks,

Spudicus87

---

<div class="post-metadata">

**Author:** ![spudicus87](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@spudicus87](https://www.ruby-forum.com/u/spudicus87)\
**Post date:** [October 11, 2008, 10:09pm UTC](https://www.ruby-forum.com/t/needing-a-little-help-with-a-ruby-program/150707/2 "2008-10-11T22:09:43Z")

</div>

On Oct 11, 2008, at 3:02 PM, Leslie Cain wrote:

> file\_size = Hash.new(0)  
> size = ‘#’ \* size / 1024
> 
> I think I have the first part and the last part down pretty well, I am  
> stuck on how to get the du output into the hash so I can output it  
> correctly. Any help would be appreciated.
> 
> Thanks,
> 
> Spudicus87

First, you might be overdoing the directory part. You may want to  
simply test File.directory?(ARGV[0])

Look at each line in the du output. If it’s anything like mine  
(MacOSX), you have a number, some spaces, and a filename. Presumably  
the filename could have embedded spaces so that may cause trouble if  
you aren’t careful.

Here are some hints:

Regexp#match (or String#=~) to catch both pieces.

String#split on the spaces between the number and the filename using  
the optional second parameter to limit the number of parts.

String#to\_i or Integer() to get an Integer from a string of digits

You might look at Kernel.sprintf or String#% to format at least the  
filename portion of the output.

You may not even need to put the data into a Hash (or Array or  
whatever) if you can process each line directly to the output that you  
want.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
