Skip to content

Instantly share code, notes, and snippets.

@cesare
Created August 8, 2012 15:49
Show Gist options
  • Save cesare/3296137 to your computer and use it in GitHub Desktop.
Save cesare/3296137 to your computer and use it in GitHub Desktop.
an example of Ruby Enumerable
class Directory
include Enumerable
def initialize(dirname)
@dirname = dirname
@files = Dir.open(dirname) {|dir|
dir.reject {|name| name == "." || name == ".." }
}
end
def each(&block)
if block_given?
@files.each do |name|
path = File.join(@dirname, name)
if File.directory?(path)
Directory.new(path).each(&block)
else
yield path
end
end
else
Enumerator.new(self, :each)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment