From: Jason Creighton Date: 2003-06-10T08:02:14+09:00 Subject: Re: [RCR] join block On Mon, 9 Jun 2003 10:21:35 +0900 Joel VanderWerf wrote: > puts([1,2,3,4,3,2,1].join do |x,y| x= " end) > > # ==> 1 < 2 < 3 < 4 >= 3 >= 2 >= 1 > # (No problem with odd-sized arrays!) > > puts([1,2,3].join(" still works ")) > > # ==> 1 still works 2 still works 3 Nice. I think that the "two elements" version would be the one to pick: (Assuming you really need this). Otherwise #join with a block would just be a wrapper around #collect and #join. But I'm not totally sure we need this at all. > The block value (in the first case) is used in exactly the same way as > the argument value (in the second case). But that's just POLS for me, > and IANYM. IANYM == "I am not Yukihiro Matsumoto"? > I just think what you're asking for shouldn't be called #join, but > something else, like #map_then_join. But why stop with join? Why not > #map_then_reverse? Maybe a more general answer would be #map_then_send, > as in: > > [1,2,3].map_then_send(:join, ", ") {|x| "<#{x}>"} > # ==> "<1>, <2>, <3>" > > Is this reductio getting close to absurdum yet? Yes. :-) We *already* have a way to chain #collect and #join. [1,2,3].collect { |x| "<#{x}>" }.join(", ") # => "<1>, <2>, <3>" I find this simple, clean and elegant. However. It seems having #join take a block might be easier sometimes. And isn't Ruby about making programming easy? OTOH, since adding an option whenever we think it might make things a little easier could lead to bloat, why don't we have 'class-name/extension-name' directories, so we could do things like this: require 'array/joinblock' And then we start putting Ruby (Or maybe C: Can you change the meaning of Array#join from a C extension? You probably can, so it wouldn't have to be in Ruby) implementations of things like a #join that accepts a block in there, and if it turns out to be fantastically useful, then move it into the Ruby interpreter. Or maybe just leave in there and let people load it up whenever they want it. And another thing: Why isn't #join in Enumerable? Jason Creighton