Why are Ruby-related types of types based on strings (line-typed)?

eg. Dir.entriesreturns an array of strings and an array containing instances of Fileor Dir. Most methods are for Dir and File types. Instances are aneamic in comparison.

No Dir#foldersor Dir#files- instead, I explicitly

  • loop over Dir.entries
  • build path ( File.expand_path) for each element
  • verify File.directory?

Simple use cases like getting all .svg files in this directory, it seems to require a few hoops / loops / checks. Am I using Ruby incorrectly or does this Ruby face seem very non-ruby?

+4
source share
2 answers

File Dir .

( ) , , , Pathname. .

Dirs Files

require 'pathname'

my_folder = Pathname.new('./')
dirs, files = my_folder.children.partition(&:directory?)
# dirs is now an Array of Pathnames pointing to subdirectories of my_folder
# files is now an Array of Pathnames pointing to files inside my_folder

.svg

- .svg, , Pathname.glob:

svg_files = Pathname.glob("folder/", "*.svg").select(&:file?)

:

class Pathname
  def files
    children.select(&:file?)
  end
end

aDir = Pathname.new('folder/')
p aDir.files.find_all{ |f| f.extname == '.svg' }

Pathname#find .

+4

, ().

.svg

svgs = Dir.glob(File.join('/path/to/dir', '*.svg'))

Windows , unixoid (Linux, MacOS...) file.svg file.svg

.svg .svg File:: FNM_CASEFOLD. .svg, **/*.svg

svgs = Dir.glob('/path/to/dir/**/*.svg', File::FNM_CASEFOLD)

, .svg,

svgs.reject! { |path| File.directory?(path) }
+1

Source: https://habr.com/ru/post/1666697/


All Articles