Converting upper-case string into title-case using Ruby

后端 未结 10 1400
小鲜肉
小鲜肉 2020-12-25 10:46

I\'m trying to convert an all-uppercase string in Ruby into a lower case one, but with each word\'s first character being upper case. Example:

convert \"MY STRING HE

10条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-25 11:47

    Capitalizes every word in a sentence using ruby, without regex.. because unfortunately those scare me

    class Book
        attr_accessor :title
        def title=(new_title)
            result = []
            words = new_title.split(' ')
            words.each do |word|
                capitalized = word[0].upcase + word[1..word.length].downcase
                result.push(capitalized)
            end
    
            @title = result.join(' ')
        end
    end
    

提交回复
热议问题