# Capitalize bug?

**URL:** <https://www.ruby-forum.com/t/capitalize-bug/75915>\
**Category:** Ruby\
**Created:** [November 14, 2006, 11:19am UTC](https://www.ruby-forum.com/t/capitalize-bug/75915 "2006-11-14T11:19:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![meddurenos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/51bf81/32.png) [@meddurenos](https://www.ruby-forum.com/u/meddurenos)\
**Post date:** [November 14, 2006, 11:19am UTC](https://www.ruby-forum.com/t/capitalize-bug/75915/1 "2006-11-14T11:19:53Z")

</div>

Hi!

I’m a new member here, though I use and enjoy ruby for about three  
years. There’s a beginning to everyuthing, including thing not doing the  
way you think they should, even in ruby!

So here’s my problem:

@nom = wnom.gsub(/\_+(.)/, ‘\1’.upcase)

What this code should do is taking the string wnom, and removing every \_  
and at the same time capitalizing the letter immediatly after.

for instance do\_it\_now would become doItNow. One way to translate ruby  
usage into camel case.

but this code does in fact producte doitnow instead of doItNow.

Now, I am not blocked, because I have achieved my goal with this code:

nom = wnom.gsub(/\_+(.)/) { |deb| deb[1, 1].upcase }

but this is less clear, and surely less efficient (although this is not  
a real problem in my case).

My question is WHY does my first code NOT do what I think it should?

Thank you for your help.

–  
Jean-Pierre

---

<div class="post-metadata">

**Author:** ![meddurenos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/51bf81/32.png) [@meddurenos](https://www.ruby-forum.com/u/meddurenos)\
**Post date:** [November 14, 2006, 11:32am UTC](https://www.ruby-forum.com/t/capitalize-bug/75915/2 "2006-11-14T11:32:06Z")

</div>

On 11/14/06, Jean-pierre Riviere [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What this code should do is taking the string wnom, and removing every \_
> 
> but this is less clear, and surely less efficient (although this is not  
> a real problem in my case).
> 
> My question is WHY does my first code NOT do what I think it should?

The problem is when the parameters are evaluated:

@nom = wnom.gsub(/\_+(.)/, ‘\1’.upcase)

in this case, ‘\1’.upcase is evaluated BEFORE the gsub is called. As  
‘\1’.upcase is equal to ‘\1’  
what you’re doing is in fact:

@nom = wnom.gsub(/\_+(.)/, ‘\1’)

in the block case, the block is evaluated AFTER the match is found and  
therefore it works.

---

<div class="post-metadata">

**Author:** ![meddurenos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/51bf81/32.png) [@meddurenos](https://www.ruby-forum.com/u/meddurenos)\
**Post date:** [November 14, 2006, 11:34am UTC](https://www.ruby-forum.com/t/capitalize-bug/75915/3 "2006-11-14T11:34:07Z")

</div>

Jean-pierre Riviere wrote:

> Hi!
> 
> I’m a new member here, though I use and enjoy ruby for about three  
> years. There’s a beginning to everyuthing, including thing not doing the  
> way you think they should, even in ruby!
> 
> So here’s my problem:
> 
> @nom = wnom.gsub(/\_+(.)/, ‘\1’.upcase)

The parameters to gsub are: 1. a regex, 2. the result of ‘\1’.upcase  
(that is, ‘\1’).

> nom = wnom.gsub(/\_+(.)/) { |deb| deb[1, 1].upcase }  
> If you want to substitute the substring with the result of applying a  
> function to it, your only choice is to use the block parameter to gsub,  
> yes. (Did the previous sentence parse?) Another possibility is to use  
> $1.upcase.

HTH

---

<div class="post-metadata">

**Author:** ![meddurenos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/51bf81/32.png) [@meddurenos](https://www.ruby-forum.com/u/meddurenos)\
**Post date:** [November 14, 2006, 11:50am UTC](https://www.ruby-forum.com/t/capitalize-bug/75915/4 "2006-11-14T11:50:12Z")

</div>

spooq wrote:

> foo = “do\_it\_now”  
> foo.gsub(/\_+(.)/, $1.upcase)  
> =\> “doNtNow”

Inside the block…

---

<div class="post-metadata">

**Author:** ![meddurenos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/51bf81/32.png) [@meddurenos](https://www.ruby-forum.com/u/meddurenos)\
**Post date:** [November 14, 2006, 11:46am UTC](https://www.ruby-forum.com/t/capitalize-bug/75915/5 "2006-11-14T11:46:11Z")

</div>

foo = “do\_it\_now”  
foo.gsub(/\_+(.)/, $1.upcase)  
=\> “doNtNow”

---

<div class="post-metadata">

**Author:** ![meddurenos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/51bf81/32.png) [@meddurenos](https://www.ruby-forum.com/u/meddurenos)\
**Post date:** [November 14, 2006, 11:50am UTC](https://www.ruby-forum.com/t/capitalize-bug/75915/6 "2006-11-14T11:50:13Z")

</div>

On 11/14/06, Jean-pierre Riviere [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> nom = wnom.gsub(/\_+(.)/) { |deb| deb[1, 1].upcase }

This is clearer… the dollar vars are assigned properly inside the  
block.

foo.gsub(/\_+(.)/) { $1.upcase }

---

<div class="post-metadata">

**Author:** ![meddurenos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/51bf81/32.png) [@meddurenos](https://www.ruby-forum.com/u/meddurenos)\
**Post date:** [November 14, 2006, 11:58am UTC](https://www.ruby-forum.com/t/capitalize-bug/75915/7 "2006-11-14T11:58:18Z")

</div>

spooq wrote:

> Now you tell me 😉

Context is all :).

---

<div class="post-metadata">

**Author:** ![meddurenos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/51bf81/32.png) [@meddurenos](https://www.ruby-forum.com/u/meddurenos)\
**Post date:** [November 14, 2006, 11:56am UTC](https://www.ruby-forum.com/t/capitalize-bug/75915/8 "2006-11-14T11:56:16Z")

</div>

On 11/14/06, Carlos [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> spooq wrote:
> 
> > foo = “do\_it\_now”  
> > foo.gsub(/\_+(.)/, $1.upcase)  
> > =\> “doNtNow”
> 
> Inside the block…

Now you tell me 😉

---

<div class="post-metadata">

**Author:** ![meddurenos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/51bf81/32.png) [@meddurenos](https://www.ruby-forum.com/u/meddurenos)\
**Post date:** [November 14, 2006, 12:42pm UTC](https://www.ruby-forum.com/t/capitalize-bug/75915/9 "2006-11-14T12:42:38Z")

</div>

spooq wrote:

> On 11/14/06, Jean-pierre Riviere [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > nom = wnom.gsub(/\_+(.)/) { |deb| deb[1, 1].upcase }
> 
> This is clearer… the dollar vars are assigned properly inside the  
> block.
> 
> foo.gsub(/\_+(.)/) { $1.upcase }

thank you for all the explanations guys.

now, I don’t know which one is clearer… which one would you recommend?  
Surely yours with $1 has a greater scope, especially if $1 recovered  
something more complex in my regexp…

Just have to get used to it, I suppose.

–  
Jean-Pierre
