Created
July 11, 2011 13:10
-
-
Save andreacfm/1075798 to your computer and use it in GitHub Desktop.
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 PersonEnumerator | |
include Enumerable | |
attr_reader :persons | |
def initialize(persons) | |
@persons = persons | |
end | |
def each &block | |
@persons.each do |person| | |
if block_given? | |
block.call person | |
else | |
yield person | |
end | |
end | |
end | |
end | |
en = PersonEnumerator.new [p1,p2,p3] | |
p en.find {|person| person.name == 'Andrea'} #<Person:0x10e61d4d8 @name="Andrea"> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment