Why does Rails titlecase add a space to a name?

前端 未结 10 1192
我在风中等你
我在风中等你 2021-02-05 10:28

Why does the titlecase mess up the name? I have:

John Mark McMillan

and it turns it into:

>> \"john mark McM         


        
10条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-05 10:46

    If all you want is to ensure that each word starts with a capital:

    class String
      def titlecase2
        self.split(' ').map { |w| w[0] = w[0].upcase; w }.join(' ')
      end
    end
    
    irb(main):016:0> "john mark McMillan".titlecase2
    => "John Mark McMillan"
    

提交回复
热议问题