Created
August 8, 2012 15:49
-
-
Save cesare/3296137 to your computer and use it in GitHub Desktop.
an example of Ruby Enumerable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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