# Needing a little help with a ruby program

**URL:** <https://rubytalk.org/t/needing-a-little-help-with-a-ruby-program/49578>\
**Category:** ruby-talk\
**Created:** [11 October 2008 18:52 UTC](https://rubytalk.org/t/needing-a-little-help-with-a-ruby-program/49578 "2008-10-11T18:52:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leslie\_Cain](https://avatars.discourse-cdn.com/v4/letter/l/f07891/32.png) [@Leslie\_Cain](https://rubytalk.org/u/Leslie_Cain)\
**Post date:** [11 October 2008 18:52 UTC](https://rubytalk.org/t/needing-a-little-help-with-a-ruby-program/49578/1 "2008-10-11T18:52:29Z")

</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|  
&nbsp;&nbsp;#Builds the hash with the filename and the sizes

end

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

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size = '#' \* size / 1024

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if times \< 0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts file +"\t | " + size

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts file

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;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:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [11 October 2008 18:58 UTC](https://rubytalk.org/t/needing-a-little-help-with-a-ruby-program/49578/2 "2008-10-11T18:58:43Z")

</div>

Study Regexp.

T.

> **···**
>
> On Oct 11, 2:52 pm, spudicu...@gmail.com wrote:
> 
> > 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 = &#39;\#&#39; \* size / 1024
> > 
> > if times &lt; 0
> > puts file \+&quot;\\t | &quot; \+ 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.
