Capitalize bug?

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

--
Posted via http://www.ruby-forum.com/.

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.

···

On 11/14/06, Jean-pierre Riviere <meddurenos@free.fr> 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)

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?

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').

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 }

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

···

--

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

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

···

On 11/14/06, Jean-pierre Riviere <meddurenos@free.fr> wrote:

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

foo = "do_it_now"
foo.gsub(/_+(.)/, $1.upcase)
=> "doNtNow"

···

On 11/14/06, Carlos <angus@quovadis.com.ar> wrote:

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').

> 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 }

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
--

spooq wrote:

···

On 11/14/06, Jean-pierre Riviere <meddurenos@free.fr> 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

--
Posted via http://www.ruby-forum.com/\.

spooq wrote:

foo = "do_it_now"
foo.gsub(/_+(.)/, $1.upcase)
=> "doNtNow"

Inside the block...

···

On 11/14/06, Carlos <angus@quovadis.com.ar> wrote:

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').

> 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 }

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
--

Now you tell me :wink:

···

On 11/14/06, Carlos <angus@quovadis.com.ar> wrote:

spooq wrote:

> foo = "do_it_now"
> foo.gsub(/_+(.)/, $1.upcase)
> => "doNtNow"

Inside the block...

spooq wrote:

···

On 11/14/06, Carlos <angus@quovadis.com.ar> wrote:

spooq wrote:

> foo = "do_it_now"
> foo.gsub(/_+(.)/, $1.upcase)
> => "doNtNow"

Inside the block...

Now you tell me :wink:

Context is all :).
--