How to find all the directories in the specific path of the ruby?

advertisements

How find all the directories in string path

EX

tmp/guava/metadata/en-US/images/icon.png

i would like to list

  • en-US
  • images

Combine String#split with File.dirname:

path = "tmp/guava/metadata/en-US/images/icon.png"

parts = File.dirname(path).split('/')
# => ["tmp", "guava", "metadata", "en-US", "images"]

parts.last(2)
# => ["en-US", "images"]