Regexpr with gsub

Hi,
I’m trying to get code to replace first alpha char for Cap, and can’t
get how it works, in case#1 dot represent any char including space and
it works fine, but when I tried to do first “a” it doesn’t catch
anything, I played with [a-z] and other syntax but still have this
problem. There is a leading space in string, but I expect ^ still to
find first occured alpha.
Can anybody point my brain into right place, why it doesn’t work?

ruby 1.9.3p0 (2011-10-30) [i386-mingw32]
irb(main):002:0> puts ss
sabaka ryba
=> nil
irb(main):003:0> puts ss.gsub(/^./,“A”) # case #1
Asabaka ryba
irb(main):005:0> puts ss.gsub(/^a/,“A”) # case #2
sabaka ryba
irb(main):005:0> puts ss.gsub(/^[a-z]/,/[A-Z]/) # case #3
sabaka ryba

Best
Dai

On Wed, Feb 8, 2012 at 10:32 AM, Vic T. [email protected] wrote:

sabaka ryba
=> nil
irb(main):003:0> puts ss.gsub(/^./,“A”) # case #1
Asabaka ryba
irb(main):005:0> puts ss.gsub(/^a/,“A”) # case #2
sabaka ryba
irb(main):005:0> puts ss.gsub(/^[a-z]/,/[A-Z]/) # case #3
sabaka ryba

irb(main):002:0> “aaa aaa”.gsub /\ba/, ‘A’
=> “Aaa Aaa”

This won’t work for the general case though. You might rather want to
do something like

irb(main):003:0> “aaa aaa”.gsub(/\w+/) {|m| m.capitalize}
=> “Aaa Aaa”

Kind regards

robert

Robert K. wrote in post #1044728:

This won’t work for the general case though. You might rather want to
do something like

irb(main):003:0> “aaa aaa”.gsub(/\w+/) {|m| m.capitalize}
=> “Aaa Aaa”

Thanks, Robert !
I need to cap only first alpha char on each line and there is leading
space, so need to check it bit more. I need somehow incorporate [a-z]
into gsub for that.

Dai

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs