Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Converting a string between camel case & snake case in Rails

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 5.97k
    Comment on it

    There is a method provided in rails to convert a string to the camel case.
    The method is camelize.


    By default camelize converts a string to upper camel case. If we don't specify any argument in camelize method, it will convert the given string to upper camel case.

    ex:-

    2.1.5 :003 > a = "hello_world".camelize
     => "HelloWorld" 
    

    If we want to print the string in lower CamelCase(i.e first character in lower case and first character of other Consecutive words in upper case) then we can pass a lower parameter to the camelize method.

    ex:-

     2.1.5 :002 > "hello_world".camelize(:lower)
     => "helloWorld"
    

    If a string is to be converted to the snake case we can use the underscore method provided by Rails. The underscore method converts a string to snake case(i.e words seperated with _)

    2.1.5 :010 > "HelloWorldTest".underscore
     => "hello_world_test"
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: