From: Damphyr Date: 2005-09-28T01:09:25+09:00 Subject: Re: Class and Iterator Design Question Jim Freeze wrote: > This may be a silly design question, but I always balk at the right > answer when I am confronted with it. > > 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: > > # Give user access to the array and let them iterate over Array class > Pea attr_reader :pods end Pea.new.pods.each { |pod| ..do stuff.. } > > or > > # Provide a custom iterator class Pea def each_pod @pods.each { |pod| > yield pod } end end Pea.new.each_pod { |pod| ..do stuff.. } end > > In other words, for classes that manage a list of items, do people > prefer to see a custom iterator, such as #each_, or do they > prefer getting back an array and iterating over it themselves, such > as #.each? > > Pea.new.each_pod { |pod| ..do stuff.. } Pea.new.pods.each { |pod| > ..do stuff.. } > Encapsulate, encapsulate, encapsulate! I prefer not exposing the innards. Define each, mixin Enumerator and let Ruby do it's magic. Cheers, V.- ____________________________________________________________________ http://www.freemail.gr - ������ �������� ������������ ������������. http://www.freemail.gr - free email service for the Greek-speaking.