Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "**/*/" and "**/"?

Tags:

ruby

Here are two ways to use glob to recursively list directories:

Dir.glob("**/*/")

Dir.glob("**/")

The output appears to be the same, at least for a small subtree. Is there a difference between those two commands I am missing out on?

like image 327
Julie Ju-Yeon Kang Avatar asked Nov 03 '22 13:11

Julie Ju-Yeon Kang


1 Answers

The ** matches 0 or more directories. By placing a * at the end you remove directories in the root, essentially making it 1 or more:

 a = Dir.glob('/tmp/**/*/').sort     
 b = Dir.glob('/tmp/**/').sort.size
 b.size => 19
 a.size => 18
 b - a =>  ["/tmp/"]

Without a leading constant path though, it doesn't look like there is a difference as 0 length matches aren't interesting and don't get put in the results.

like image 142
Paul Rubel Avatar answered Nov 13 '22 15:11

Paul Rubel