Say i have an array, a= ["aa"' "bb", "cc", "d", "e"]
How can i delete any instances of a single character in the array,
without specifically specifying what they are, with something like
a.delete("d","e")?
On Mon, Apr 30, 2012 at 5:19 PM, Ok Ok <lists@ruby-forum.com> wrote:
Here is my question...
Say i have an array, a= ["aa"' "bb", "cc", "d", "e"]
How can i delete any instances of a single character in the array,
without specifically specifying what they are, with something like
a.delete("d","e")?
On Mon, Apr 30, 2012 at 5:19 PM, Ok Ok <lists@ruby-forum.com> wrote:
Here is my question...
Say i have an array, a= ["aa"' "bb", "cc", "d", "e"]
How can i delete any instances of a single character in the array,
without specifically specifying what they are, with something like
a.delete("d","e")?
No need for a block. We can have it a bit more efficient
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 /(..)./m, '\\1'
=> "th qic bow fx ums ve te az dg"
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"
Kind regards
robert
···
On Mon, Apr 30, 2012 at 7:11 PM, Brian Candler <lists@ruby-forum.com> wrote:
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?
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
···
On Tue, May 1, 2012 at 9:44 AM, Brian Candler <lists@ruby-forum.com> 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?