Ross Bamford
2/22/2006 5:07:00 PM
On Thu, 2006-02-23 at 01:18 +0900, James Byrne wrote:
> Ross Bamford wrote:
> >
> > 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.
>
> Yes, it does help, very much so. The .move method is essentially
> private, although not presently declared as such. The intermediate
> methods called from outside the instance are .get and .put which, on
> reflection, are probably more suitable places to check the object class
> of the passed directory arguments. That simplfies the assignments in the
> move class as you recommend.
Well, glad to be able to help :)
I would just say though that I was actually advocating *not* checking
the arguments, but instead going for more of a duck-typing
implementation.
I'm assuming that the class you're working on looks something like this:
class DirectoryThing
def initialize(dir)
@dir = dir
end
def move(sourcedir,globspec,targetdir,suf=nil)
# .. code above
end
def path
@dir
end
# ... etc ...
end
And that you want to be able to pass strings, or other DirectoryThings,
as the arguments. So if you have:
class DirectoryThing
def to_s
self.path
end
end
You can confidently to_s the argument you're passed (as above), rather
than having to explicitly check what it is - a string will give you
itself, while a DirectoryThing will give you it's path.
Just a thought...
--
Ross Bamford - rosco@roscopeco.REMOVE.co.uk