When I do,
> "fooo".gsub("o") {puts "Found an 'o'"}
Found an 'o'
Found an 'o'
Found an 'o'
=> "f"
gsub removes all 'o's. How does this work?
I think gsub passes each character to the block, but since block is doing nothing to the character itself (like catching it), it is dropped.
I think this is the case because, when I do
> "fooo".gsub("o"){|ch| ch.upcase}
=> "fOOO"
the block is catching the character and turning it into uppercase. But when I do,
> "fooo".gsub("o", "u"){|ch| ch.upcase}
=> "fuuu"
How does Ruby handle the block in this case?
I found that Ruby plugs the blocks into methods using yield. (check this) But I am still not sure about my explanation for the first code example and third example. Can anyone put some more light on this?