From: Rob Biedenharn Date: 2009-12-31T05:53:59+09:00 Subject: Re: awk print $4 in ruby, NEW QUESTION? On Dec 30, 2009, at 3:10 PM, Derek Smith wrote: >> Rob Biedenharn wrote: >> Rob@AgileConsultingLLC.com > > Hi again, > > Thank you Rob, quite the explanation! > I did exactly as you said and it seems my loop is not even running: > > > [root@vir/usr/local/vrep/OS_scripts]# ruby filesys_chk.rb / > entering to_s > # @filesystem="/", @rawdisk=nil> > > class FsData > def initialize(filesystem) > @rawdisk = @used = @available = @capacity = nil > @filesystem = filesystem > Take this test... > if ARGV.empty? > $stderr.puts "An argument of filesystem is required to run > this script, bye!" > exit 1 > end ...and put it outside the class. > end > > def rundf > puts "in dfrun method" > df = %x(df -m |tail +2) > df.each do |ln| > df_fields = ln.chomp.split > next unless df_fields.last == @filesystem > > @rawdisk = df_fields[1] > @used = df_fields[2] > @available = df_fields[3] > @capacity = df_fields[4] > end > end > This is begin executed during the parsing of the class definition, not when the to_s method is called. Since you already have the "in to_s method" in the right place, I'd just remove the "entering to_s" line completely. > puts "entering to_s" > def to_s > puts "in to_s method" > str = "#{@filesystem} :" > str << " raw #{@rawdisk}" if @rawdisk > str << " used #{@used}" if @used > str << " available #{@available}" if @available > str << " capacity #{@capacity}" if @capacity > if @available <= 4557 > puts "YESSSSSSSSSSSSSSSSSSSSS!!!!!!!!!!" > end > end > end > > p FsData.new(ARGV[0]) This creates the FsData object, but never asks it to do the rundf method. The end of your script (i.e., following the class definition) should be: if ARGV.empty? $stderr.puts "An argument of filesystem is required to run this script, bye!" exit 1 end p FsData.new(ARGV[0]).rundf Or instead of the p (perhaps better): fs = FsData.new(ARGV[0]) fs.rundf puts fs You ought to get output like: in dfrun method in to_s method YESSSSSSSSSSSSSSSSSSSSS!!!!!!!!!! / : raw 123456 used 120000 available 3456 capacity 95% But, of course, I'm just making up the numbers. -Rob > > -- > Posted via http://www.ruby-forum.com/. > Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com