# Special Array method about Unix pathes

**URL:** <https://www.ruby-forum.com/t/special-array-method-about-unix-pathes/115010>\
**Category:** Ruby\
**Created:** [September 19, 2007, 6:03pm UTC](https://www.ruby-forum.com/t/special-array-method-about-unix-pathes/115010 "2007-09-19T18:03:58Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Une\_BSSSSvS\_ue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/2acd7d/32.png) [@Une\_BSSSSvS\_ue](https://www.ruby-forum.com/u/Une_BSSSSvS_ue)\
**Post date:** [September 19, 2007, 6:03pm UTC](https://www.ruby-forum.com/t/special-array-method-about-unix-pathes/115010/1 "2007-09-19T18:03:58Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![Une\_BSSSSvS\_ue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/2acd7d/32.png) [@Une\_BSSSSvS\_ue](https://www.ruby-forum.com/u/Une_BSSSSvS_ue)\
**Post date:** [September 19, 2007, 6:32pm UTC](https://www.ruby-forum.com/t/special-array-method-about-unix-pathes/115010/2 "2007-09-19T18:32:10Z")

</div>

Une Bévue wrote:

> “/Volumes/EMTEC KEY/emtec\_dl/Win98SE new driver.exe” « belongs »
> 
> end  
> 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?

---

<div class="post-metadata">

**Author:** ![Une\_BSSSSvS\_ue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/2acd7d/32.png) [@Une\_BSSSSvS\_ue](https://www.ruby-forum.com/u/Une_BSSSSvS_ue)\
**Post date:** [September 19, 2007, 6:38pm UTC](https://www.ruby-forum.com/t/special-array-method-about-unix-pathes/115010/3 "2007-09-19T18:38:12Z")

</div>

Charles D Hixson wrote:

> > ```
> > self.each { | x |
> > return true if path.startsWith?(x)
> > }
> > return false
> > 
> > ```

self.any? {|x| path.starts\_with?(x) }

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![Une\_BSSSSvS\_ue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/2acd7d/32.png) [@Une\_BSSSSvS\_ue](https://www.ruby-forum.com/u/Une_BSSSSvS_ue)\
**Post date:** [September 19, 2007, 7:06pm UTC](https://www.ruby-forum.com/t/special-array-method-about-unix-pathes/115010/4 "2007-09-19T19:06:20Z")

</div>

Sebastian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> self.any? {|x| path.starts\_with?(x) }

fine thanks, i didn’t know the #any?

---

<div class="post-metadata">

**Author:** ![Une\_BSSSSvS\_ue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/2acd7d/32.png) [@Une\_BSSSSvS\_ue](https://www.ruby-forum.com/u/Une_BSSSSvS_ue)\
**Post date:** [September 19, 2007, 7:07pm UTC](https://www.ruby-forum.com/t/special-array-method-about-unix-pathes/115010/5 "2007-09-19T19:07:20Z")

</div>

Charles D Hixson [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Array doesn’t seem the proper class to have a includePath? method,  
> otherwise the names look reasonable.

i’ll lokk if there is something more related to Unix pathes…

> If the method is in Array, then a  
> reasonable name might be includesStart?

OK, thanks!

---

<div class="post-metadata">

**Author:** ![Une\_BSSSSvS\_ue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/2acd7d/32.png) [@Une\_BSSSSvS\_ue](https://www.ruby-forum.com/u/Une_BSSSSvS_ue)\
**Post date:** [September 19, 2007, 8:26pm UTC](https://www.ruby-forum.com/t/special-array-method-about-unix-pathes/115010/6 "2007-09-19T20:26:57Z")

</div>

Alle mercoledì 19 settembre 2007, Une Bévue ha scritto:

> “/Volumes/EMTEC KEY/emtec\_dl/Win98SE new driver.exe” « belongs »
> 
> end  
> 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 ?

This is my alternative solution:

def select\_toplevel\_paths arg  
a = arg.dup.sort\_by{|i| i.size}  
a.inject([]) do |res, i|  
res.any?{|j| i.index(j) == 0 } ? res : (res \<\< i)  
end  
end

It doesn’t change the contents of the array and it’s not a method of  
class  
Array, because, in my opinion, it’s a too specialized method to be put  
in a  
core class.

Regarding your code, I’d say names are correct, as far as English is  
concerned. I’d like to give you a copule of suggestions:

- use underscores instead of camel-case (include\_path? instead of  
includePath,  
for example), because it’s the more common style in the ruby community
- use start\_with? instead of starts\_with?. In the ruby standard library,  
there  
are several cases in which both methods exist, with the ones ending in s  
being  
obsolete (for example, File.exists? and File.exist?) For consistency, I  
think  
it’s better to follow the same convention.

Stefano

---

<div class="post-metadata">

**Author:** ![Une\_BSSSSvS\_ue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/2acd7d/32.png) [@Une\_BSSSSvS\_ue](https://www.ruby-forum.com/u/Une_BSSSSvS_ue)\
**Post date:** [September 19, 2007, 8:44pm UTC](https://www.ruby-forum.com/t/special-array-method-about-unix-pathes/115010/7 "2007-09-19T20:44:58Z")

</div>

Hi,

At Thu, 20 Sep 2007 03:24:58 +0900,  
Stefano C. wrote in [ruby-talk:269890]:

> def select\_toplevel\_paths arg  
> a = arg.dup.sort\_by{|i| i.size}  
> a.inject([]) do |res, i|  
> res.any?{|j| i.index(j) == 0 } ? res : (res \<\< i)

i.rindex(j, 0) is faster than i.index(j) == 0 when j matches in  
the middle.

> - use start\_with? instead of starts\_with?. In the ruby standard library, there  
> are several cases in which both methods exist, with the ones ending in s being  
> obsolete (for example, File.exists? and File.exist?) For consistency, I think  
> it’s better to follow the same convention.

And ruby 1.9 has String#start\_with? already in fact.

---

<div class="post-metadata">

**Author:** ![Une\_BSSSSvS\_ue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/2acd7d/32.png) [@Une\_BSSSSvS\_ue](https://www.ruby-forum.com/u/Une_BSSSSvS_ue)\
**Post date:** [September 19, 2007, 9:31pm UTC](https://www.ruby-forum.com/t/special-array-method-about-unix-pathes/115010/8 "2007-09-19T21:31:11Z")

</div>

Stefano C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This is my alternative solution:
> 
> def select\_toplevel\_paths arg  
> a = arg.dup.sort\_by{|i| i.size}  
> a.inject([]) do |res, i|  
> res.any?{|j| i.index(j) == 0 } ? res : (res \<\< i)  
> end  
> end

clever solution, thanks a lot !

however with the following (part of a yaml file) :

forbiddenList:

- /Volumes/EMTEC KEY/emtec\_dl
- /Volumes/EMTEC KEY/emtec\_dl/v233r001/Setup.exe
- /Volumes/U3 System
- /Volumes/U3 System/Launchpad.zip

your select\_toplevel\_paths don’t remove /Volumes/EMTEC  
KEY/emtec\_dl/v233r001/Setup.exe and /Volumes/U3 System/Launchpad.zip

> It doesn’t change the contents of the array and it’s not a method of class  
> Array, because, in my opinion, it’s a too specialized method to be put in a  
> core class.

U’re right, i was embarassed with that point.

> Regarding your code, I’d say names are correct, as far as English is  
> concerned. I’d like to give you a copule of suggestions:
> 
> - use underscores instead of camel-case (include\_path? instead of includePath,  
> for example), because it’s the more common style in the ruby community
> - use start\_with? instead of starts\_with?. In the ruby standard library, there  
> are several cases in which both methods exist, with the ones ending in s being  
> obsolete (for example, File.exists? and File.exist?) For consistency, I think  
> it’s better to follow the same convention.

OK, done !

---

<div class="post-metadata">

**Author:** ![Une\_BSSSSvS\_ue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/2acd7d/32.png) [@Une\_BSSSSvS\_ue](https://www.ruby-forum.com/u/Une_BSSSSvS_ue)\
**Post date:** [September 19, 2007, 8:46pm UTC](https://www.ruby-forum.com/t/special-array-method-about-unix-pathes/115010/9 "2007-09-19T20:46:59Z")

</div>

2007/9/19, Sebastian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Charles D Hixson wrote:
> 
> > > self.each { | x |  
> > > return true if path.startsWith?(x)  
> > > }  
> > > return false
> 
> self.any? {|x| path.starts\_with?(x) }

You can even leave the “self.” out. 🙂

I can think of several approaches to solve this:

1. order the array and do a second iteration where each item is  
checked against the last seen prefix; the idea is that every string  
comes before the strings that contain it as prefix.

2. insert names into a tree and output only prefixes during a tree walk.

Bévue, the only issue I see with your method naming is that in Ruby  
methods typically use under\_scores and not CamelCase as in Java.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Une\_BSSSSvS\_ue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/2acd7d/32.png) [@Une\_BSSSSvS\_ue](https://www.ruby-forum.com/u/Une_BSSSSvS_ue)\
**Post date:** [September 19, 2007, 9:33pm UTC](https://www.ruby-forum.com/t/special-array-method-about-unix-pathes/115010/10 "2007-09-19T21:33:12Z")

</div>

Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You can even leave the “self.” out. 🙂

OK !

> I can think of several approaches to solve this:
> 
> 1. order the array and do a second iteration where each item is  
> checked against the last seen prefix; the idea is that every string  
> comes before the strings that contain it as prefix.

fine, the Array is allready ordered elsewhere, i thought putting sort!  
in this method ))

> 1. insert names into a tree and output only prefixes during a tree walk.

ok, i see what you mean, may be better not to do that when output rather  
than when building the three having a means to compare three deepness.

> Bévue, the only issue I see with your method naming is that in Ruby  
> methods typically use under\_scores and not CamelCase as in Java.

Yes i’ve corrected that, it’s an habit coming from java, as the getter  
“getContainers!”…

---

<div class="post-metadata">

**Author:** ![Une\_BSSSSvS\_ue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/2acd7d/32.png) [@Une\_BSSSSvS\_ue](https://www.ruby-forum.com/u/Une_BSSSSvS_ue)\
**Post date:** [September 19, 2007, 9:44pm UTC](https://www.ruby-forum.com/t/special-array-method-about-unix-pathes/115010/11 "2007-09-19T21:44:14Z")

</div>

Alle mercoledì 19 settembre 2007, Une Bévue ha scritto:

> forbiddenList:
> 
> - /Volumes/EMTEC KEY/emtec\_dl
> - /Volumes/EMTEC KEY/emtec\_dl/v233r001/Setup.exe
> - /Volumes/U3 System
> - /Volumes/U3 System/Launchpad.zip
> 
> your select\_toplevel\_paths don’t remove /Volumes/EMTEC  
> KEY/emtec\_dl/v233r001/Setup.exe and /Volumes/U3 System/Launchpad.zip

It works for me:

irb: 001\> def select\_toplevel\_path arg  
irb: 002+\> a = arg.dup.sort\_by{|i| i.size}  
irb: 003+\> a.inject([]) do |res, i|  
irb: 004\*\> res.any?{|j| i[0, j.size] == j} ? res : (res \<\< i)  
irb: 005+\> end  
irb: 006+\> end  
nil  
irb: 007\> forbidden = ["/Volumes/EMTEC KEY/emtec\_dl",  
irb: 008\*\> “/Volumes/EMTEC KEY/emtec\_dl/v233r001/Setup.exe”,  
irb: 009\*\> “/Volumes/U3 System”,  
irb: 010\*\> “/Volumes/U3 System/Launchpad.zip”  
irb: 011+\> ]  
["/Volumes/EMTEC KEY/emtec\_dl", “/Volumes/EMTEC  
KEY/emtec\_dl/v233r001/Setup.exe”, “/Volumes/U3 System”, “/Volumes/U3  
System/Launchpad.zip”]  
irb: 012\> select\_toplevel\_path forbidden  
["/Volumes/U3 System", “/Volumes/EMTEC KEY/emtec\_dl”]

Notice that this doesn’t change the forbidden array, but creates a new  
one.

Stefano

---

<div class="post-metadata">

**Author:** ![Une\_BSSSSvS\_ue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/2acd7d/32.png) [@Une\_BSSSSvS\_ue](https://www.ruby-forum.com/u/Une_BSSSSvS_ue)\
**Post date:** [September 19, 2007, 10:11pm UTC](https://www.ruby-forum.com/t/special-array-method-about-unix-pathes/115010/12 "2007-09-19T22:11:16Z")

</div>

Une Bévue [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> KEY/emtec\_dl/v233r001/Setup.exe and /Volumes/U3 System/Launchpad.zip  
> sorry, in fact that works wheb used as :

forbidden = [  
“/Volumes/U3 System”,  
“/Volumes/EMTEC KEY/emtec\_dl”,  
“/Volumes/EMTEC KEY/emtec\_dl/Win98SE new driver.exe”,  
“/Volumes/EMTEC KEY/emtec\_dl/v233r001/Setup.exe”,  
“/Volumes/EMTEC KEY/emtec\_dl/v233r001”,  
“/Volumes/U3 System/LaunchU3.exe”,  
“/Volumes/U3 System/Launchpad.zip”  
]

p forbidden  
forbidden = select\_toplevel\_paths( forbidden )  
p forbidden

but not when used in a class :

class SyncDataSource \< OSX::NSObject

```
def initialize

```

[…]

```
    @forbiddenList = []

```

[…]

```
end

```

[…]

```
def addForbidden( notification )

```

[…]

```
    p @forbiddenList
    @forbiddenList = select_toplevel_paths( @forbiddenList )
    p @forbiddenList

```

[…]

```
end

```

end

i don’t understand why for the time being…

---

<div class="post-metadata">

**Author:** ![Une\_BSSSSvS\_ue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/2acd7d/32.png) [@Une\_BSSSSvS\_ue](https://www.ruby-forum.com/u/Une_BSSSSvS_ue)\
**Post date:** [September 19, 2007, 10:55pm UTC](https://www.ruby-forum.com/t/special-array-method-about-unix-pathes/115010/13 "2007-09-19T22:55:24Z")

</div>

On 19.09.2007 21:26, Une Bévue wrote:

> Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I can think of several approaches to solve this:
> > 
> > 1. order the array and do a second iteration where each item is  
> > checked against the last seen prefix; the idea is that every string  
> > comes before the strings that contain it as prefix.
> 
> fine, the Array is allready ordered elsewhere, i thought putting sort!  
> in this method ))

If it’s sorted anyway that’s probably the approach I’d take.

> > 1. insert names into a tree and output only prefixes during a tree walk.
> 
> ok, i see what you mean, may be better not to do that when output rather  
> than when building the three having a means to compare three deepness.

I am not sure what you mean here. You have to build the tree first  
before you can decide which branches you can omit. With “output” I did  
not necessarily mean output to screen, but rather output from the  
method. Maybe I should have chosen a different wording. I am sorry if  
that caused confusion.

> > Bévue, the only issue I see with your method naming is that in Ruby  
> > methods typically use under\_scores and not CamelCase as in Java.
> 
> Yes i’ve corrected that, it’s an habit coming from java, as the getter  
> “getContainers!”…

Different languages, different conventions. 🙂

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Une\_BSSSSvS\_ue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/2acd7d/32.png) [@Une\_BSSSSvS\_ue](https://www.ruby-forum.com/u/Une_BSSSSvS_ue)\
**Post date:** [September 20, 2007, 5:02pm UTC](https://www.ruby-forum.com/t/special-array-method-about-unix-pathes/115010/14 "2007-09-20T17:02:47Z")

</div>

Une BÃ©vue \<unbewusst.sein wortanschahung.com.invalid\> writes:

> ```
> self.delete_if { | x | ( self - [x] ).includePath?( x ) }
> 
> ```
> 
> end  
> end

In contrast to everyone else’s points about your naming, I’m not happy  
with your  
implementation.

/home/gareth/files/my\_file  
is not a child of  
/home/gareth/fil

Gareth

---

<div class="post-metadata">

**Author:** ![Une\_BSSSSvS\_ue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/2acd7d/32.png) [@Une\_BSSSSvS\_ue](https://www.ruby-forum.com/u/Une_BSSSSvS_ue)\
**Post date:** [September 20, 2007, 7:46am UTC](https://www.ruby-forum.com/t/special-array-method-about-unix-pathes/115010/15 "2007-09-20T07:46:12Z")

</div>

Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ok, i see what you mean, may be better not to do that when output rather  
> > than when building the three having a means to compare three deepness.
> 
> I am not sure what you mean here. You have to build the tree first  
> before you can decide which branches you can omit. With “output” I did  
> not necessarily mean output to screen, but rather output from the  
> method. Maybe I should have chosen a different wording. I am sorry if  
> that caused confusion.

i’ve found a simpler approach, in fact, this list of pathes is entered  
by the user using a File/folder chooser window, then instead of walking  
a three or something else, xhen the user enter a new path right now i  
check only if this path is allready taken into account by :

files.each { |f|  
f\_to\_s = f.to\_s  
@forbiddenList \<\< f\_to\_s unless @forbiddenList.include\_path?( f\_to\_s )  
}

with :  
files the File(s)/Folder(s) entered by the user ;  
f.to\_s converting NSString to Ruby String (NS stands for NextStep) ;  
Array#include\_path?( a\_path ) being :  
class Array  
def include\_path?(path)  
self.any? { | x | a\_path.rindex( x, 0 ) }  
end  
end

the only thing i dislike is in that pathes have nothing to do with  
Array,i must rexrite this part.

> > > Bévue, the only issue I see with your method naming is that in Ruby  
> > > methods typically use under\_scores and not CamelCase as in Java.
> > 
> > Yes i’ve corrected that, it’s an habit coming from java, as the getter  
> > “getContainers!”…
> 
> Different languages, different conventions. 🙂

yes right, here i’m embarassed because my code isn’t “pure” Ruby, it’s  
RubyCocoa (a bridge between Cocoa/ObjC on Mac OS X) and in RubyCocoa the  
Cocoa methods like :

NSOpenPanel#beginSheetForDirectory:file:types:modalForWindow:modalDelega  
te:didEndSelector:contextInfo:

are translated, on the Ruby side, in :

beginSheetForDirectory\_file\_types\_modalForWindow\_modalDelegate\_didEndSel  
ector:contextInfo( … )

then “\_” replaces “:” and still camelized…
