Skip to content

Instantly share code, notes, and snippets.

@mehdi-farsi
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mehdi-farsi/86c711bd22ff9f678bf6 to your computer and use it in GitHub Desktop.
Save mehdi-farsi/86c711bd22ff9f678bf6 to your computer and use it in GitHub Desktop.
Fetch filenames using filter and REGEX
# In the following example some simple default options are set.
class Dir
@list_options = { hidden_files: /^\./, visible_files: /^[^\.]/, files: // }
def self.list(path = '.', option = :visible_files)
entries(path).grep(@list_options[option])
end
end
# Examples
Dir.list('.', :hidden_files).each { |file| print "#{file} " }; puts
Dir.list('.', :visible_files).each { |file| print "#{file} " }; puts
Dir.list('.', :files).each { |file| print "#{file} " }; puts
# OUTPUT
$> ruby dir.rb
. .. .DS_Store
dir.rb test.json
. .. .DS_Store dir.rb test.json
$>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment