Jesús Gabriel y Galán
5/13/2008 9:42:00 AM
On Tue, May 13, 2008 at 5:22 AM, Clement Ow
<clement.ow@asia.bnpparibas.com> wrote:
> Jes=FAs Gabriel y Gal=E1n wrote:
> > On Mon, May 12, 2008 at 11:00 AM, Clement Ow
> > <clement.ow@asia.bnpparibas.com> wrote:
> >>
>
> >> sd_a.each do |sd|
> >> $source, $destination, $selections, $file_exception =3D sd
> >> src =3D File.join $source, $selections
> >> puts src
> >> d=3D $d1
> >> dst=3D File.join $destination, d
> >> test =3D File.join $source, $file_exception
> >
> > Untested but change this:
> >
> >> src1 =3D Dir.glob(src) - Dir.glob(test)
> >
> > to:
> >
> > src1 =3D $file_exception.inject(Dir.glob(src)) {|result, ex| result -
> > Dir.glob(ex)}
> >
> > Also, doing a Dir.glob for each exception can be a lot, why not use
> > regular expressions
> > to remove from the result of the first glob? You will have to change
> > the exceptions
> > a little bit, but it might be worth it:
> >
> > ["\.txt", "\.sql"].inject(Dir.glob("/home/jesus/*")) {|result, ex|
> > result.reject{|x| x =3D~ Regexp.new(ex)}}
> >
> > This removes from my home folder all files that match ".txt" and ".sql=
"
> >
> > Hope this helps,
> >
> > Jesus.
>
> Hi Jesus,
> nice slick code there using regexp ;) just wondering if there would be a
> possibility of having a different set of file exceptions for different
> file paths. cause at the moment, there can only be one specific file
> exceptions for every file path, which might be hard in the event where
> we would need to cater to different source paths. Prolly a few arrays in
> file exception?
If what you want is to associate different information to each source path,
I would look into a hash of hashes or a hash of arrays, or a hash of struct=
s,
where you could have a complex object for the info related to an entity of
your program. For example:
A hash of arrays, if you only need exceptions:
exceptions_by_path =3D {}
exceptions_by_path["/home/jesus"] =3D ["\.txt", "\.sql"]
exceptions_by_path["/home/jesus/applications"] =3D ["\.sh", "\.bin"]
# [...]
and then
paths.each do |path|
files =3D exceptions_by_path.inject(Dir.glob("#{path}/*")) {|result,
ex| result.reject{|x| x =3D~ Regexp.new(ex)}}
end
If for a path you need several different things, I would go with a
Struct or a custom class:
FileInfo =3D Struct.new :exceptions, :other_value, :yet_another
paths_info =3D {}
paths_info["/home/jesus"] =3D FileInfo.new(["\.txt", "\.sql"], "other
value", "another")
and then access the exceptions array as paths_info["/home/jesus"].exception=
s
and use it as before.
Hope this helps,
Jesus.