From: Robert Klemme Date: 2012-05-02T01:15:11+09:00 Subject: Re: delete character from array On Tue, May 1, 2012 at 9:44 AM, Brian Candler wrote: > Robert Klemme wrote in post #1059013: >> No need for a block. > > I used the block form because I believe it to be clearer (no issues > about backslash escaping) > >> But this deletes every third _character_.  It will work the same for >> OP's strings but may not work in other cases.  For really deleting >> _letters_ we could do >> >> irb(main):004:0> c=0; a.gsub(/\w/) {|ch| (c+=1) % 3 == 0 ? nil : ch} >> => "th quck ron fx jmp ovr te lzy og" > > Ah, but aren't all those repeated method calls for integer addition and > modulo arithmetic going to be inefficient? :-) > > You could use a single gsub instead: > >>> a.gsub(/(\w[^\w]*\w[^\w]*)\w([^\w]*)/, '\\1\\2') > => "th quck ron fx jmp ovr te lzy og" Oh, yes of course: much better! And we can get rid of all those brackets and a bit more: irb(main):001:0> a = "the quick brown fox jumps over the lazy dog" => "the quick brown fox jumps over the lazy dog" irb(main):002:0> a.gsub /(\w\W*\w\W*)\w/, '\\1' => "th quck ron fx jmp ovr te lzy og" Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/