"roktas" <roktas / omu.edu.tr> wrote in message news:<20020225143216.A1028 / WIN2K>...
> * kwatch <kwatch / lycos.jp> [020225 14:20]:
> > If you know how to copy a directory recursively with ruby,
> > please teach me.
> > I can't find method like 'copy' or 'xcopy' in Dir class.
> 
> How about this one? (`recursive file copy routine` from rubycook)
> 
> http://www.rubycookbook.org/showrecipe.rb?recipeID=36
> 


Thanks roktas,
You means that ruby doesn't have directory-copy routine or method, don't you?

Routine which roktas introduced can work fine if
	cp_r("/some/where/dir", "dir2")
but does not work if
	cp_r("dir", "dir2")


So, I wrote a directory-copy routine named 'xcopy':
------------------------------------------------
###
### xcopy(src, dest)
###
def xcopy(src, dest)
    if (test(?d, src))
        # copy directory
        if test(?e, dest)
            ## should I raise exception?
            print "Error: file or directory '#{dest}' already exist.\n"
            return false
        else
            Dir.mkdir(dest)
        end
        Dir.foreach(src){|f|
            next if (f == '.' || f == '..')
            xcopy("#{src}/#{f}", "#{dest}/#{f}")
        }
    else
        # copy file
        begin
            src_f = open(src)
            dest_f = open(dest, "w")
            dest_f.write(src_f.read)
        ensure
            src_f.close if src_f
            dest_f.close if dest_f
        end
    end
end
------------------------------------------------


There are some problems on this routine:
 * it is procedural, not object-oriented
 * cannot think of permission
 * if file is very large, code 'dest_f.write(src_f.read)' will require
   very large memory.


Tell me your advice if you have a good idea and enough time :-)


Regards,
kwatch