File globbing in Ruby and Unix -


i have following directory structure on desktop:

sample/   a.rb   b.rb   c.rb   sub_sample/     blah.rb   whatever/     meow.rb 

unix globbing behaves expected in bash:

desktop $ ls sample/*.rb sample/a.rb sample/b.rb sample/c.rb  desktop $ ls sample/*/*.rb sample/sub_sample/blah.rb   sample/whatever/meow.rb  desktop $ ls sample/**/*.rb sample/sub_sample/blah.rb   sample/whatever/meow.rb 

apparently, last example recursively glob, similar ruby, if globstar enabled.

here how globbing works in ruby (last example produces different output):

>> dir[dir.home + "/desktop/sample/*.rb"] => ["/users/powers/desktop/sample/a.rb", "/users/powers/desktop/sample/b.rb", "/users/powers/desktop/sample/c.rb"]  >> dir[dir.home + "/desktop/sample/*/*.rb"] => ["/users/powers/desktop/sample/sub_sample/blah.rb", "/users/powers/desktop/sample/whatever/meow.rb"]  # recursive output don't understand >> dir[dir.home + "/desktop/sample/**/*.rb"] => ["/users/powers/desktop/sample/a.rb", "/users/powers/desktop/sample/b.rb", "/users/powers/desktop/sample/c.rb", "/users/powers/desktop/sample/sub_sample/blah.rb", "/users/powers/desktop/sample/whatever/meow.rb"] 

when more 1 star used, behaves same single star:

>> dir[dir.home + "/desktop/sample/***************/*.rb"] => ["/users/powers/desktop/sample/sub_sample/blah.rb", "/users/powers/desktop/sample/whatever/meow.rb"] 

here questions:

  1. how ** recursively search through folders? think of * matches of length. ** interpreted entirely different (like method)?

  2. are there other ways recursively glob in ruby? seems /**/* 'standard' way glob in ruby, syntax bit confusing me. can more comfortable when figure out ** doing.

thanks.

saying dir['path/**/*.rb'] is, more or less, saying:

find path -name '*.rb' 

from shell. ruby's dir, ** glob searches specified path , directories under path. apparently bash's ** looks @ directories under path without looking @ files in path @ all.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -