Hi guys,
I wanted to avoid using the word “block” as it’s such a key word in
Ruby.
I’m performing the same test twice here and there must be a way to
reuse it…
=begin
Mon Jan 21 12:51:19 GMT 2008
simple plays with dirs
=end
create a “singleton object”
testobj = Object.new
play with the object - just a start
def testobj.homePlay
startDir = Dir.pwd
p “current dir is #{startDir}”
puts “Dir.getwd == Dir.pwd” if Dir.getwd == Dir.pwd
check for ~/
if Dir.exist?("#{ENV[“HOME”]}")
p “changing to ~/”
else
p “cannot find your home dir. STOP!”
exit
end
Dir.chdir
have we changed directory from where we started?
if Dir.pwd == startDir
p “current dir is still #{startDir}. pwd not changed ;-)”
else
p “pwd has changed to Dir.pwd”
end
print “file listing of your home dir…\n\n”
p Dir.entries(".").sort # << this an array
print “\n\nlisting …/ \n\n”
Dir.foreach("…") {|x| p x}
end
end of method testobj.dirPlay
def testobj.procPlay
px = “/proc/”
if Dir.exist?("#{px}")
p “#{px} dir found - continue”
else
p “#{px} not found - STOP!”
exit
end
end
send message to object
testobj.homePlay
testobj.procPlay
END
The question is how to reuse the “testing if directory exists chunk of
code”? I’m still thinking procedurally I guess.
Regards,
John M.
MSc (DIC)
+44 7739 171 531