From: "Jesús Gabriel y Galán" Date: 2008-05-13T18:41:46+09:00 Subject: Re: Handling of arrays On Tue, May 13, 2008 at 5:22 AM, Clement Ow wrote: > Jesús Gabriel y Galán wrote: > > On Mon, May 12, 2008 at 11:00 AM, Clement Ow > > wrote: > >> > > >> sd_a.each do |sd| > >> $source, $destination, $selections, $file_exception = sd > >> src = File.join $source, $selections > >> puts src > >> d= $d1 > >> dst= File.join $destination, d > >> test = File.join $source, $file_exception > > > > Untested but change this: > > > >> src1 = Dir.glob(src) - Dir.glob(test) > > > > to: > > > > src1 = $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 =~ 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 structs, 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 = {} exceptions_by_path["/home/jesus"] = ["\.txt", "\.sql"] exceptions_by_path["/home/jesus/applications"] = ["\.sh", "\.bin"] # [...] and then paths.each do |path| files = exceptions_by_path.inject(Dir.glob("#{path}/*")) {|result, ex| result.reject{|x| x =~ Regexp.new(ex)}} end If for a path you need several different things, I would go with a Struct or a custom class: FileInfo = Struct.new :exceptions, :other_value, :yet_another paths_info = {} paths_info["/home/jesus"] = FileInfo.new(["\.txt", "\.sql"], "other value", "another") and then access the exceptions array as paths_info["/home/jesus"].exceptions and use it as before. Hope this helps, Jesus.