Camelize up to a certain part of a string

  • A+
Category:Languages

I have:

s = "like_so__case" 

camelize gives this:

s.camelize # => "LikeSoCase" 

I'm looking for conversion up to a double underscore __ to get:

"LikeSo__case" 

How can I camelize only up to a certain part of a string?


The simplest option is to gsub part of your string.

'like_so__case'.gsub(/(.*?)(__.*)/) { "#{$1.camelize}#{$2}" } #=> "LikeSo__case" 

UPDATE

Cleaner and faster way arising from comments.

'like_so__case__h'.sub(/(.*?)(?=__)/, &:camelize) #=> "LikeSo__case__h" 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: