From: Brian Candler Date: 2004-10-21T05:03:17+09:00 Subject: Re: each with separator > My inclination would be to start simple: > > module Enumerable I suppose, from what I was saying before about it being impolite to pollute system classes (giving rise to potential name conflicts), that I ought to separate it out of Enumerable. module SuperEnumerable # choose a unique name here def separate_by(separator) Separator.new(self,separator) end class Separator include Enumerable def initialize(thing,separator) @thing = thing @separator = separator end def each first = true @thing.each do |item| yield @separator unless first first = false yield item end end end end a = [1,2,3] a.extend SuperEnumerable a.separate_by("-").each {|obj| puts obj} b = a.separate_by("-").collect {|obj| obj * 2} p b You can always include SuperEnumerable in class Array if you wish, but you're not required to. Regards, Brian.