Ruby:扩展String类以将每个单词大写

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ruby:扩展String类以将每个单词大写相关的知识,希望对你有一定的参考价值。

Don't forget to use require 'directory/name_of_class'
  1. # This helper is opening up core Ruby String class
  2. # in order to add a new method to all Strings
  3.  
  4. class String
  5.  
  6. # Ruby has a capitalize method (used below) which capitalizes the
  7. # first letter of a string. But in order to capitalize the first
  8. # letter of EVERY word we have to write our own.
  9.  
  10. def titleize
  11. self.split(' ').collect {|word| word.capitalize}.join(" ")
  12. end
  13.  
  14. end

以上是关于Ruby:扩展String类以将每个单词大写的主要内容,如果未能解决你的问题,请参考以下文章

扩展 ruby​​ 特征类以加载 CarrierWave

如何使用ruby / rails将所有大写字母转换为适当的首字母大写字母,其余为小写?

ruby--string常用方法

如何大写字符串中每个单词的第一个字符

Java如何将每个单词的第一个字符转为大写

Ruby中的大写,交换大小写和反转[重复]