From: Charles D Hixson Date: 2007-09-20T01:31:42+09:00 Subject: Re: special Array method about Unix pathes Une B�vue wrote: > here is the goal: > > i've an Array of Unix pathes : > forbidden = ["/Volumes/U3 System", > "/Volumes/EMTEC KEY/emtec_dl", > "/Volumes/EMTEC KEY/emtec_dl/Win98SE new driver.exe", > "/Volumes/EMTEC KEY/emtec_dl/v233r001" > ] > > i want to forbid access to those pathes BUT since, for example, > "/Volumes/EMTEC KEY/emtec_dl/Win98SE new driver.exe" � belongs � > to the path (it's below this path) "/Volumes/EMTEC KEY/emtec_dl" > it is unusefull to have it in the Array. > > in the case above i'd like to return : > > forbidden = ["/Volumes/U3 System", > "/Volumes/EMTEC KEY/emtec_dl" > ] > > what i've done : > > > class Array > def includePath?(path) > self.each { | x | > return true if path.startsWith?(x) > } > return false > end > def getContainers! > self.delete_if { | x | ( self - [ x ] ).includePath?( x ) } > end > end > > class String > def startsWith?(dir) > self.index(dir) === 0 > end > end > > then forbidden.getContainers! gives me the expected result. > > what do you think about this solution ? > could you propose a better one ? > > also, because english isn't my mother tongue, are my methods naming > correct ? > > Array doesn't seem the proper class to have a includePath? method, otherwise the names look reasonable. If the method is in Array, then a reasonable name might be includesStart?