On Dec 29, 2009, at 3:24 PM, Derek S. wrote:
Mike S. wrote:
Thanks to all who replied! 
I am trying to understand why my code is not entering my rundf method?
Well, mostly because you never tell it to. 
end
I’d suggest that this is not right for the initialize method. (Mostly
because of the ‘exit 1’, but also because the FsData probably
shouldn’t refer to ARGV at all).
How about:
def initialize(filesystem)
@rawdisk = @used = @available = @capacity = nil
@filesystem = filesystem
end
end
puts @available
if @available <= 4557
p "#{@available} is low as testing"
end
end
Ooh, I’ll come back to this…
end
p dff = FsData.new
if ARGV.empty?
$stderr.puts “The filesystem is required as an argument to this
script”
exit 1
end
dff = FsData.new(ARGV[0])
dff.rundf
OUTPUT
ruby filesys_chk.rb /
#<FsData:0x8115370 @filesystem=“/”, @rawdisk=“”, @capacity=“”,
@available=“”, @used=“”>
OK, now back to that rundf method. I’m assuming that:
- the calls to split are OK (I think you mean to get 0…4 not 1…5,
but your df output wins)
- the three fields where you use to_i need it
- you mean to get just the line for the given filesystem
- your df output doesn’t have any spaces in the non-numeric fields
that mess up String#split
Now, understand that the original filesystem provided is doing nothing
(and is overwritten if the df.each loop runs).
def rundf
puts “entering rundf 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].to_i
@used = df_fields[2].to_i
@available = df_fields[3].to_i
@capacity = df_fields[4]
end
p "#{@available} is low as testing" if @available && @available
<= 4557
@available
end
And perhaps a #to_s method as well:
def to_s
str = “#{@filesystem} :”
str << " raw #{@rawdisk}" if @rawdisk
str << " used #{@used}" if @used
str << " available #{@available}" if @available
str << " capacity #{@capacity}" if @capacity
str
end
I’m relying on the fact that in my version of your FsData class, the
instance variables are initialized to nil (not an empty string).
Others may not like the approach to building the string representation
in parts in this case, but it illustrates a more general technique for
when those variables might not all exist at the same time.
The end of the script could then be:
puts FsData.new(ARGV[0])
You could also treat the whole exercise as a class method, give rundf
an argument (the filesystem), and call:
puts FsData.rundf(ARGV[0])
and not bother with an instance at all (since you don’t do anything
else with it).
-Rob
Rob B. http://agileconsultingllc.com
[email protected]