Camelize (camel case) ruby string

First letter: upper

def camelize(term); term.to_s.gsub(/(?:^|_+)([^_])/) { $1.upcase } end

First letter: lower

def camelize(term)
  term.to_s.gsub(/(?:^|_+)([^_])/) { $1.upcase }
    .tap { |s| s[0] = s[0].downcase }
end

Code snippets in this post are covered by 0BSD License.


Tags
ruby snippet

Date
October 30, 2022