Skip to content
Convert strings between different cases.
Ruby Shell
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Change to move github templates Nov 20, 2019
benchmarks Add benchmarks to compare with ActiveSupport::Inflector Nov 9, 2019
bin Add initial project setup Nov 8, 2019
LICENSE.txt Change docs to use correct method name Dec 5, 2019
Rakefile Add initial project setup Nov 8, 2019
strings-case.gemspec Add initial project setup Nov 8, 2019

strings logo


Gem Version Build Status Build status Maintainability Coverage Status Inline docs

Convert strings to different cases.

Strings::Case provides string case conversions for Strings utilities.


Popular solutions that deal with transforming string cases work well in simple cases.(Sorry ;-) With more complex strings you may get unexpected results:

ActiveSupport::Inflector.underscore("supports IPv6 on iOS?")
# => "supports i_pv6 on i_os?"

In contrast, Strings::Case aims to be able to transform any string to expected case:

Strings::Case.underscore("supports IPv6 on iOS?")
# => "supports_ipv6_on_ios"


Add this line to your application's Gemfile:

gem 'strings-case'

And then execute:

$ bundle

Or install it yourself as:

$ gem install strings-case


  • No monkey-patching String class
  • Converts any string to specified case
  • Supports Unicode characters
  • Provides many common case transformations
  • Allows to preserve casing of acronyms


1. Usage

The Strings::Case is a module with functions for transforming between string cases:

Strings::Case.snakecase("foo bar baz")
# => "foo_bar_baz"

It will transform any string into expected case:

Strings::Case.snakecase("supports IPv6 on iOS?")
# => "supports_ipv6_on_ios"

You can apply case transformations to Unicode characters:

# => "здравствуйте_привет"

Here is a quick summary of available transformations:

Case Type Result
Strings::Case.camelcase("foo bar baz") "fooBarBaz"
Strings::Case.constcase("foo bar baz") "FOO_BAR_BAZ"
Strings::Case.headercase("foo bar baz") "Foo-Bar-Baz"
Strings::Case.kebabcase("foo bar baz") "foo-bar-baz"
Strings::Case.pascalcase("foo bar baz") "FooBarBaz"
Strings::Case.pathcase("foo bar baz") "foo/bar/baz"
Strings::Case.sentencecase("foo bar baz") "Foo bar baz"
Strings::Case.snakecase("foo bar baz") "foo_bar_baz"
Strings::Case.titlecase("foo bar baz") "Foo Bar Baz"

2. API

2.1 camelcase

To convert a string into a camel case, that is, a case with all the words capitilized apart from the first one and compouned together without any space use camelase method. For example:

Strings::Case.camelcase("HTTP Response Code")
# => "httpResponseCode"

To preserve the acronyms use the :acronyms option:

Strings::Case.camelcase("HTTP Response Code", acronyms: ["HTTP"])
# => "HTTPResponseCode"

2.2 constcase

To convert a string into a constant case, that is, a case with all the words uppercased and separated by underscore character use constcase. For example:

Strings::Case.constcase("HTTP Response Code")

2.3 headercase

To covert a string into a header case, that is, a case with all the words capitalized and separated by a hypen use headercase. For example:

Strings::Case.headercase("HTTP Response Code")
# => "Http-Response-Code"

To preserve the acronyms use the :acronyms option:

Strings::Case.headercase("HTTP Response Code", acronyms: ["HTTP"])
# => "HTTP-Response-Code"

2.4 kebabcase | dashcase

To convert a string into a kebab case, that is, a case with all the words lowercased and separted by a dash, like a words kebabab on a skewer, use kebabcase or dashcase methods. For example:

Strings::Case.kebabcase("HTTP Response Code")
# => "http-response-code"

To preserve the acronyms use the :acronyms option:

Strings::Case.dashcase("HTTP Response Code", acronyms: ["HTTP"])

expect(dashed).to eq("HTTP-response-code")

2.5 pascalcase

To convert a string into a pascal case, that is, a case with all the words capitilized and compounded together without a space, use pascalcase method. For example:

Strings::Case.pascalcase("HTTP Response Code")
# => "HttpResponseCode"

To preserve the acronyms use the :acronyms option:

Strings::Case.pascalcase("HTTP Response Code")
# => "HTTPResponseCode"

2.6 pathcase

To convert a string into a file path use pathcase:

Strings::Case.pathcase("HTTP Response Code")
# => "http/response/code"

To preserve the acronyms use the :acronyms option:

Strings::Case.pathcase("HTTP Response Code", acronyms: ["HTTP"])
# => "HTTP/response/code"

By default the / is used as a path separator. To change this use a :sep option. For example, on Windows the file path separator is \:

Strings::Case.pathcase("HTTP Response Code", separator: "\\")
# => "http\response\code"

2.7 sentencecase

To turn a string into a sentence use sentencecase:

Strings::Case.sentencecase("HTTP Response Code")
# => "Http response code"

To preserve the HTTP acronym use the :acronyms option:

Strings::Case.sentencecase("HTTP Response Code", acronyms: ["HTTP"])
# => "HTTP response code"

2.8 snakecase | underscore

To convert a string into a snake case by lowercasing all the characters and separating them with an _ use snakecase or underscore methods. For example:

Strings::Case.snakecase("HTTP Response Code")
# => "http_response_code"

To preserve acronyms in your string use the :acronyms option. For example:

Strings::Case.snakecase("HTTP Response Code", acronyms: ["HTTP"])
# => "HTTP_response_code"

2.9 titlecase

To convert a string into a space delimited words that have their first letter capitalized use titlecase. For example:

# => "Http Response Code"

To preserve the HTTP acronym use the :acronyms option:

Strings::Case.titlecase("HTTP response code", acronyms: ["HTTP"])
# => "HTTP Response Code"

3. Extending String class

Though it is highly discouraged to pollute core Ruby classes, you can add the required methods to String class by using refinements.

For example, if you wish to only extend strings with snakecase method do:

module MyStringExt
  refine String do
    def snakecase(*args)
      Strings::Case.snakecase(self, *args)

Then snakecase method will be available for any strings where refinement is applied:

using MyStringExt

"foo bar baz".snakecase
# => "foo_bar_baz"

However, if you want to include all the Strings::Case methods, you can use provided extensions file:

require "strings/case/extensions"

using Strings::Case::Extensions


After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to


Bug reports and pull requests are welcome on GitHub at This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.


The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Strings::Case project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.


Copyright (c) 2019 Piotr Murach. See LICENSE for further details.

You can’t perform that action at this time.