On Mon, Aug 13, 2007 at 06:00:13AM +0900, Jon Hawkins wrote:
> i need to be able to run my program fully from the command line, instead
> of 'ruby program.rb'....i need to pipe the commands using standard input
> and output.
> How would i do this with the code below?
> 
> Code:
> 
> require 'find'
> Dir.chdir("/")
> res = []
> Find.find("/") {|path| res << File.size( path).to_f if File.file?(
> path)}
> puts "Total Number of Files: #{res.length}"
> puts "Average File Size: #{res.inject(0) {|sum, i| sum +
> i}/res.length/1024} Kilobytes"
> puts "Max File Size: #{res.max/1024}KB"
> puts "Min File Size: #{res.min}KB"
> puts "================================"
> puts "================================"
> res.delete_if {|b| b < 1}
> puts "Weighted Average: #{res.inject(0) {|sum, i| sum +
> i}/res.length/1024} Kilobytes"
> 
>  # Lol Standard Deviation
> 
> def mean(res)
> res.inject(0) {|sum, x| sum += x} / res.size.to_f / 1024
> end
> def mean_and_standard_deviation(res)
> m = mean(res)
> variance = res.inject(0) {|variance, x| variance += (x - m) ** 2}
> Math.sqrt(variance/(res.size - 1)) / 1024
> end
> puts "Standard Deviation = #{mean_and_standard_deviation(res)}"
> ##############
> 
> So how would i pipe find from a shell command?
> Thanks,

I'm not quite sure if I know what your question is.  What do you want to
pipe into or out of your ruby program.  Do you want to do something
like:

    find /some/path -type f | ruby program.rb

and have ruby calculate sizes on your files and output summary
information?

If that is the case then the file names appear on stdin of the ruby
program and you can do:

    $stdin.each_line do |file_path|
        res << File.size(file_path)
    end

Is that what you are looking for?

enjoy,

-jeremy

-- 
========================================================================
 Jeremy Hinegardner                              jeremy / hinegardner.org