From: Ross Bamford Date: 2006-02-23T00:16:53+09:00 Subject: Re: A small refractory problem On Wed, 2006-02-22 at 23:48 +0900, James Byrne wrote: > I have this code: > > def move(sourcedir,globspec,targetdir,suf=nil) > > if sourcedir.kindof?(self.class) then sourcedir = sourcedir.path end > sourcedir = File.expand_path(sourcedir.to_s) > if !self.validpath?(sourcedir) then > raise ArgumentError, "Source directory invalid", caller > end > > if targetdir.kindof?(self.class) then targetdir = targetdir.path end > targetdir = File.expand path(targetdir.to_s) > if !self.validpath?(targetdir) then > raise ArgumentError, "Target directory invalid", caller > end > > I would like reduce the duplicate statements into a single block > iterated over for source and target. But I lack the ruby syntax > knowledge to determine how best to go about this. I would appreciate > some examples of how this could be done using an array or a hash and > possibly employing symbols. source and target can be an object of the > same class or something that can meaningfully converted into a string > containing a directory path. Maybe something like: def move(sourcedir,globspec,targetdir,suf=nil) [sourcedir,targetdir].each do |dir| dir = File.expand_path(dir.to_s) raise ArgumentError, "#{dir} not valid" unless validpath?(sourcedir) end end Not sure how the rest of your code works, but it seems to me you could have whatever class this is return 'path' from to_s, avoiding the check you were doing (and avoiding having to set vars outside the block). Hope that helps. -- Ross Bamford - rosco@roscopeco.REMOVE.co.uk