Guest User

Untitled

a guest
Feb 21st, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. # This gets called as Image.from("directory"){|i| some_code}
  2.  
  3. def self.scan(directory, debug = false, &block)
  4. puts block.inspect if block
  5. return nil unless File.directory?(directory)
  6. tens = 0; count = 0
  7. Dir.entries(directory).map do |file|
  8. if count == 10
  9. tens += 1; count = 0
  10. print "."; STDOUT.flush
  11. end
  12. next if [".", ".."].include?(file) or File.directory?(file)
  13. if block_given?
  14. i = Image.ping("#{directory}/#{file}") rescue next
  15. # next unless yield i.first
  16. next unless block.call(i.first).call
  17. else
  18. Image.ping("#{directory}/#{file}") rescue next
  19. end
  20. # This might be handy in large directories...
  21. GC.start
  22. count += 1 if debug
  23. "#{directory}/#{file}"
  24. end.compact
  25. end
  26.  
  27. def self.from(collection, &block)
  28. if collection.is_a?(String)
  29. collection = (block) ? scan(collection) { block } : scan(collection)
  30. end
  31. puts collection.inspect
  32. i = Image.read(collection.random)
  33. return i if i.size > 1
  34. i.first
  35. end
Add Comment
Please, Sign In to add comment