I'm building a stack of paths that each lead to a template directory. The idea is to crawl up the tree pushing paths onto the stack as long as there are contents in the template directory. This is an example file tree:$ tree .. stack.rb templates  gem   optionparser    template        foo.erb   template       foo.erb  template test_stack.rb6 directories, 4 filesThis is how I am currently solving the problem:def templates_stack template_path  stack = []  while Dir[template_path + /*].any?    stack.unshift(File.expand_path(template_path))    template_path = File.expand_path(../../template, template_path)  end  stackendp templates_stack(templates/gem/optionparser/template)# => [/Users/max/Dropbox/work/tmp/template_stack/templates/gem/template,#     /Users/max/Dropbox/work/tmp/template_stack/templates/gem/optionparser/template]The main problem is my code is not describing the real intent behind the method. The main thing I want is a stack of paths to a template directory that all aren't empty But I'm having trouble communicating that clearly in my code.For example, File.expand_path(../../template... is the only spot I really declare that I'm interested in directories named template.Also, I'm using File.expand_path twice but in slightly different contexts. In reality I don't need the absolute path I just used it the first time because I needed a way to crawl up the tree the second time. I think the templates_stack method should actually return:p templates_stack(templates/gem/optionparser/template)# => [templates/gem/template, templates/gem/optionparser/template]But I'm not sure how to crawl up the tree without File.expand_pathWould love guidance on those two improvements or other areas where readability and intent can be improved.
Declarative method to return a stack of paths
ruby