This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.
| Finding the Files you Want | ||
|---|---|---|
| Code | Expected | Actual |
require 'find'
module Find
def match(*paths)
matched = []
find(*paths) { |path| matched << path if yield path }
return matched
end
module_function :match
end
require 'create_tree'
create_tree './' =>
[ { 'Music' => ['cancelled_download.MP3',
['The Snails - Red Rocket.mp3', 'Song contents #1'],
['The Snails - Moonfall.mp3', 'Song contents #2']
]
},
{ 'tmp' => ['empty1', 'empty2', ['README', 'Hi there!']] },
{ 'rubyprog-0.1' => [['rubyprog.rb', '#!/usr/bin/env ruby'],
['README', 'This Ruby program is great!']
]
}
] |
Find the empty files. |
Error! (Exception?) Here's stdout: LoadError: no such file to load -- create_tree from (irb):10:in `require' from (irb):10 NoMethodError: undefined method `create_tree' for main:Object from (irb):22 |
Find.match('./') { |p| File.lstat(p).size == 0 } |
["./tmp/empty2", "./tmp/empty1", "./Music/cancelled_download.MP3"] | [] |
Find.match('./') { |p| ext = p[-4...p.size]; ext && ext.downcase == '.mp3' } |
["./Music/The Snails - Red Rocket.mp3", | [] |
Find.match('./') { |p| File.split(p)[1] == 'README' } |
["./tmp/README", "./rubyprog-0.1/README"] | [] |
Find.match('./') do |p|
Find.prune if p == "./tmp"
File.split(p)[1] == "README"
end |
["./rubyprog-0.1/README"] | [] |
must_start_with = "This Ruby program"
Find.match('./') do |p|
if File.file? p
open(p) { |f| f.read(must_start_with.size) == must_start_with }
else
false
end
end |
["./rubyprog-0.1/README"] | [] |
def emacs_droppings(*paths)
Find.match(*paths) do |p|
(p[-1] == ?~ and p[0] != ?~) or (p[0] == ?# and p[-1] == ?#)
end
end |
Finds all files that are larger than a certain threshold. Use this to find the files hogging space on your filesystem. |
|
def bigger_than(bytes, *paths)
Find.match(*paths) { |p| File.lstat(p).size > bytes }
end |
Finds all files modified more recently than a certain number of seconds ago. |
|
def modified_recently(seconds, *paths)
time = Time.now - seconds
Find.match(*paths) { |p| File.lstat(p).mtime > time }
end |
Finds all files that haven't been accessed since they were last modified. |
|
def possibly_abandoned(*paths)
Find.match(*paths) { |p| f = File.lstat(p); f.mtime == f.atime }
end |
nil | |