Sha256: 276bf6d18482a902825aef56bf140a276ce4d077e80558b92c6eb918c6bf1a42

Contents?: true

Size: 1.59 KB

Versions: 17

Compression:

Stored size: 1.59 KB

Contents

#--
# Author::    Tyler Rick
# Copyright:: Copyright (c) 2007 QualitySmith, Inc.
# License::   Ruby License
# Submit to Facets?::
# Developer notes::
# Changes::
#++


require 'find'
require 'enumerator'

module Find
  # Identical to +find+ except +select+ returns the matching files as an _array_. (+find+ returns nil, which is not very useful if you actually wanted an array.)
  #
  # Calls the associated block with the name of every file and directory listed as arguments, then recursively on their subdirectories, and so on.
  #
  # Return a true (non-false) value from the block for every path that you want to be returned in the resulting array.
  #
  # You can still use <tt>Find.prune</tt>.
  #
  def self.select(*paths, &block)
    Enumerable::Enumerator.new(self, :find, *paths).select{|value| yield value}
  end
end






#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test
require 'test/unit'

class TheTest < Test::Unit::TestCase
  # to do: would probably be safer if we *created* our own files/directories during setup, rather than expecting there to be some for us to use in cwd
  
  def test_true
    dir = '.'
    files_expected = []
    Find.find(dir) { |path| files_expected << path }

    files = Find.select(dir) { |path| true }
    assert_equal files_expected, files

    # Doesn't have to be true necessarily -- can be a string
    files = Find.select(dir) { |path| path }
    assert_equal files_expected, files
  end

  def test_false
    dir = '.'
    files = Find.select(dir) { |path| false }

    assert_equal [], files
  end

end
=end


Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
quality_extensions-1.4.0 lib/quality_extensions/find/select.rb
quality_extensions-1.3.2 lib/quality_extensions/find/select.rb
quality_extensions-1.3.1 lib/quality_extensions/find/select.rb
quality_extensions-1.3.0 lib/quality_extensions/find/select.rb
quality_extensions-1.1.6 lib/quality_extensions/find/select.rb
quality_extensions-1.1.3 lib/quality_extensions/find/select.rb
quality_extensions-1.0.3 lib/quality_extensions/find/select.rb
quality_extensions-1.1.4 lib/quality_extensions/find/select.rb
quality_extensions-0.1.1 lib/qualitysmith_extensions/find/select.rb
quality_extensions-0.1.2 lib/quality_extensions/find/select.rb
quality_extensions-1.1.0 lib/quality_extensions/find/select.rb
quality_extensions-1.1.2 lib/quality_extensions/find/select.rb
quality_extensions-1.1.1 lib/quality_extensions/find/select.rb
quality_extensions-1.0.0 lib/quality_extensions/find/select.rb
quality_extensions-0.1.4 lib/quality_extensions/find/select.rb
qualitysmith_extensions-0.0.63 lib/qualitysmith_extensions/find/select.rb
qualitysmith_extensions-0.0.64 lib/qualitysmith_extensions/find/select.rb