giant robots smashing into other giant robots

We are thoughtbot. We make web & mobile apps.

Tagged:

Comments (View)

gsub with a block

Did you know gsub can take a block? I didn’t until today.

"10 comments".gsub(/ (.+)/) do |words|
  " <div class='superstyle'>#{words.lstrip}</div>"
end

# "10 <div class='superstyle'>comments</div>"

I like giving the match an intention-revealing name instead of $1.

However, it seems limited.

I would have expected $1 to be the first block parameter, $2 to be the second block parameter, etc., but I have only been able to get one block variable.

The More You Know