From: Dave Thomas Date: 2000-12-14T15:45:27+09:00 Subject: [ruby-talk:7200] Re: Harmful preselection by yielding? Aleksi Niemel� writes: > So, do you prefer to "fix in" the yielding instead of returning the > produced data structure. And which is the criteria you base your > decision on (feeling goes, but something more tangible would be > great)? If I hadn't been lazy, I'd have written def getpwent result = block_given? || [] File.foreach("/etc/passwd") do |line| fields = line.split(/:/) if block_given? yield fields else result << fields end end return result end And give the user the choice. getpwent { |login, pw, uid| puts "#{login}-#{uid}" } or pwd = getpwent In general, I find that I'm using iterator forms more and more. For many applications, they tend to provide a natural flow to the application. Of course, there'll often be times where it's more convenient to have all the data lying around. So I guess the decision comes down primarily to the data itself: is it inherently a single-pass scan, or something that you'll be traversing many times. And of course, in this particular case, I'd probably use the Etc module as eban suggested and do it properly ;-) Dave