capitalize bug?

  • Thread starter Jean-pierre Riviere
  • Start date
J

Jean-pierre Riviere

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

Jan Svitok

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?

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

Carlos

Jean-pierre Riviere said:
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
--
 
S

spooq

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

Jean-pierre Riviere said:
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
 
C

Carlos

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

Inside the block...
Jean-pierre Riviere said:
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
 
S

spooq

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

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

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

Jean-pierre Riviere

spooq said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top