From: Joel VanderWerf Date: 2003-06-09T10:21:35+09:00 Subject: Re: [RCR] join block Simon Strandgaard wrote: > On Sun, 08 Jun 2003 22:00:10 +0000, Martin DeMello wrote: > > >>Simon Strandgaard <0bz63fz3m1qt3001@sneakemail.com> wrote: >> >>>I know this has been discussed before, but I don't >>>know what the conclution were.. What is your opinions ? >> >>Don't think the thread reached a definite conclusion, but I'm with Joel >>on this - the join block should take in a pair of elements, and return >>the substring separating them in the result. > > > > Thinking of what will be most usable in practice: > > So far I cannot think of any cases where I need to "join-pairs into a > string". Can you show some more examples in order to convince me, that this > actualy is usable ? :-) > > > I earlier showed you some situations where 'yield each element and join' > could be useful. I don't have any compelling examples. It's just that if someone said "Now join takes a block", this is what I would guess it would do: class Array alias old_join join def join(str=nil) if block_given? raise if str # can't think what else to do result = "" curr = at(0) prev = nil (length-1).times do |idx| prev = curr curr = at(idx+1) result << prev.to_s result << yield(prev, curr).to_str end result << at(-1).to_s else old_join(str) end end end 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 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. 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?