Converting string from snake_case to CamelCase in Ruby

前端 未结 10 1083
情书的邮戳
情书的邮戳 2020-11-30 19:57

I am trying to convert a name from snake case to camel case. Are there any built-in methods?

Eg: \"app_user\" to \"AppUser\"

(I hav

10条回答
  •  抹茶落季
    2020-11-30 20:42

    I got here looking for the inverse of your question, going from camel case to snake case. Use underscore for that (not decamelize):

    AppUser.name.underscore # => "app_user"

    or, if you already have a camel case string:

    "AppUser".underscore # => "app_user"

    or, if you want to get the table name, which is probably why you'd want the snake case:

    AppUser.name.tableize # => "app_users"

提交回复
热议问题