Jes Jes - 7 months ago 16
Ruby Question

Replacement with regular expression and capture

The method below is supposed to transform

"snake_case"
to
"CamelCase"
.

def zebulansNightmare(string)
string.gsub(/_(.)/){$1.upcase}
end


With string
"camel_case"
, I expect
gsub(/_(.)/)
to match
c
after the
_
. I understood that
$1
is the first matched letter: the capital letter. But it works like it's substituting
_
with the capital letter. Why has the
_
disappeared?

Answer

You are right that $1 is the captured value, however, the gsub matches the letter with _ before it, and the whole match gets replaced. You need to reinsert _ to the result:

"camel_case".gsub(/_(.)/){"_#{$1.upcase}"}

See the IDEONE demo

BTW, if you only plan to match _ followed with a letter (so as not to waste time and resources on trying to turn non-letters to upper case), you can use the following regex:

/_(\p{Ll})/

Where \p{Ll} is any lowercase Unicode letter.