2

In my Rails project in application.rb file there is a line, which is written by some previous developer who was working on the project.

config.autoload_paths += Dir[Rails.root.join('app', 'classes', '{**}')]

I know that autoload_paths is used by rails to load all required files. I am not able to figure out the meaning of {**}. Does this means all files and sub directories of classes directory will be loaded? Is there any documentation which I can refer for this.

I have done some debugging.

2.2.5 :008 > Rails.root.join('app', 'classes', '{**}')
 => #<Pathname:/home/tk/src/project-name/app/classes/{**}> 

This is actually a Pathname object. But I have not found any reference about {**} here.

Does any one have any idea what is {**}? Is there any documentation for this?

dnsh
  • 3,516
  • 2
  • 22
  • 47

2 Answers2

4

Pathname just builds a path, it does not care about parts.

** is a parameter to Dir#[] which is eventually an alias to Dir#glob.

** means “match directories recursively.”

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
  • 1
    Also, `{..}` is used for grouping and alternation in globs (e.g. `{foo,ba{r,z},q*}` should match `foo`, `bar`, `baz`, `quux` but not `charlie`). `{**}` should be identical to `**`. – Amadan Nov 17 '16 at 07:50
  • @Amadan yes, indeed, `{**}` **is** identical to `**`. – Aleksei Matiushkin Nov 17 '16 at 07:52
2

This is a feature of Dir.glob, though I'm not sure why this is used the way it is here. {...} is a grouping mechanism, but there's only one element in it, **, which is a recursive matcher.

You should be fine with:

Dir[Rails.root.join('app', 'classes', '**')]

Or if you like something more conscise:

Dir[Rails.root.join(*%w[ app classes ** ]]
tadman
  • 208,517
  • 23
  • 234
  • 262