[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: A small refractory problem

Ross Bamford

2/22/2006 3:17:00 PM

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



3 Answers

James Byrne

2/22/2006 4:22:00 PM

0

James Byrne wrote:
> That simplfies the assignments in the move class as you recommend.
>

move method rather than move class is what I intended to write.


--
Posted via http://www.ruby-....


Ross Bamford

2/22/2006 5:07:00 PM

0

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



James Byrne

2/22/2006 5:21:00 PM

0

Ross Bamford wrote:

>
> 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...

And a very good one at that. I can see that I have much to learn. This
approach makes many things very simple. Thank you.

--
Posted via http://www.ruby-....