From: daz Date: 2003-06-12T01:53:30+09:00 Subject: Re: [RCR] join block "Joel VanderWerf" wrote: > [...] 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 [..] One option would be to pass the sepString into the block. No, wait! Don't run away. Wholly inspired by your idea:- (using Enumerable#inject - in later Rubys) ##### class Array alias :join_orig :join def join(*args) if block_given? ret_ = '' inject(false) do |x, y| ret_ << (x==false ? nil : yield(x, y, args)).to_s << y.to_s y end ret_ else join_orig(args[0] || $,) end end end puts([0,1,2,3].join) svsep = $, ; $, = '....+....'; puts([0,1,2,3].join); $, = svsep puts([0,1,2,3].join(" still works ")) puts puts('{' << [1,2,3,"a","b",5,"c"].join(',', '} {') {|x, y, z| (x.class == y.class) ? z[0] : z[1] } << '}') puts('{' << [1,2,3,"a","b",5,"c"].join do |x, y| (x.class == y.class) ? ',' : '} {' end << '}') puts # "Joel's Jewel" (as he might prefer it :', '<') {|x,y,z| " #{ z[x<=>y] } "}) # [(v2.0 beta : DfB) - "I knew that 'spaceship' was there for a reason"] ##### #-> 0123 #-> 0....+....1....+....2....+....3 #-> 0 still works 1 still works 2 still works 3 #-> #-> {1,2,3} {a,b} {5} {c} #-> {1,2,3} {a,b} {5} {c} #-> #-> 1 < 2 < 3 < 4 == 4 > 3 < 7 == 7 > 1 If it comes to a vote on this, I would like to nominate JVW as my proxy. Had he not mentioned pairs, this thread might have closed after a manageable number of comments :-) daz (- Retiring with no hint of an opinion. I'm working on a separate RCR concerning '_with_index' which I'm hoping will give a whole lot of support to the '*_w_i' crew - pres. BlackD. who IMHO has been treated scathingly on this matter . It'll require max. input from all quarters so that if YM says YAGNI, we can say YWFWA :!)