Converting string from snake_case to CamelCase in Ruby

Converting string from snake_case to CamelCase in Ruby

Add Comment


  • 4 Answer(s)

    Rail using, String#camelize

    "active_record".camelize # => "ActiveRecord"
    "active_record".camelize(:lower) # => "activeRecord"
    

    And actual class or,

    "app_user".camelize.constantize
    

    Answered on January 8, 2019.
    Add Comment

    Follow this command:  

    "hello_world".split('_').collect(&:capitalize).join #=> "HelloWorld"
    

    Answered on January 8, 2019.
    Add Comment

    They classify It handles edge cases states that he wants to convert app_user to AppUser not App_user,

    "app_user".classify # => AppUser
    "user_links".classify # => UserLink
    

    Answered on January 8, 2019.
    Add Comment

    If you’re using Rails, String#camelize is what you’re looking for.

      "active_record".camelize                # => "ActiveRecord"
      "active_record".camelize(:lower)        # => "activeRecord"

    If you want to get an actual class, you should use String#constantize on top of that.

    "app_user".camelize.constantize
    Answered on March 5, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.