This may be a silly design question, but I always balk at
the right answer when I am confronted with it.
=20
I have a class that manages a list and users need to iterate
over that list.
The way I see it, I have to basic alternatives:
=20
# Give user access to the array and let them iterate over
Array
class Pea
attr_reader
ods
end
Pea.new.pods.each { |pod| ..do stuff.. }
=20
or
=20
# Provide a custom iterator
class Pea
def each_pod
@pods.each { |pod| yield pod }
end
end
Pea.new.each_pod { |pod| ..do stuff.. }
end
=20
In other words, for classes that manage a list of items,
do people prefer to see a custom iterator, such as
#each_<item>,
or do they prefer getting back an array and iterating
over it themselves, such as #<items>.each?
=20
Pea.new.each_pod { |pod| ..do stuff.. }
Pea.new.pods.each { |pod| ..do stuff.. }
=20
Cheers