Ruby on Rails uncapitalize first letter

前端 未结 10 1467
感动是毒
感动是毒 2021-02-03 19:48

I\'m running Rails 2.3.2.

How do I convert \"Cool\" to \"cool\"? I know \"Cool\".downcase works, but is there a Ruby/Rails method

相关标签:
10条回答
  • 2021-02-03 20:20

    If you use Ruby Facets, you can lowercase the first letter:

    https://github.com/rubyworks/facets/blob/master/lib/core/facets/string/uppercase.rb

    0 讨论(0)
  • 2021-02-03 20:30

    There is also:

    "coolat_cat".camelize(:lower) # => "coolCat"
    
    0 讨论(0)
  • 2021-02-03 20:30

    There is no real inverse of capitalize, but I think underscore comes close.

    "CoolCat".underscore  #=> "cool_cat"
    "cool_cat".capitalize #=> "Cool_cat"
    "cool_cat".camelize   #=> "CoolCat"
    

    Edit: underscore is of course the inverse of camelize, not capitalize.

    0 讨论(0)
  • 2021-02-03 20:31

    There is an inverse of capitalize called swapcase:

    "Cool Cat".swapcase   #=> "cOOL cAT"
    
    0 讨论(0)
提交回复
热议问题