Skip to content
Switch branches/tags

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time
strings logo


Gem Version Actions CI 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 14.4?")
# => "supports i_pv6 on i_os 14.4?"

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

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


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 class with methods for converting between string cases:

strings =
# => "foo_bar_baz"

As a convenience, you can call methods directly on a class:

# => "foo_bar_baz"

It will transform any string into expected case:

strings.snakecase("supports IPv6 on iOS?")
# => "supports_i_pv6_on_i_os"

You can also specify acronyms as a method parameter:

strings.snakecase("supports IPv6 on iOS?", acronyms: %w[IPv6 iOS])
# => "supports_ipv6_on_ios"

To make acronyms available for all conversions, configure them once on an instance:

strings.configure do |config|
  config.acronym "IPv6"
  config.acronym "iOS"

strings.snakecase("supports IPv6 on iOS?")
# => "supports_ipv6_on_ios"

It also supports converting 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 configure

To make acronyms available for all conversions, configure them once on an instance:

strings =
strings.configure do |config|
  config.acronym "HTTP"
  config.acronym "XML"

  # or config.acronym "HTTP", "XML"

This will result in a conversion preserving acronyms like so:

# => "XMLHTTPRequest"

2.2 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 camelcase 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.3 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.4 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.5 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.6 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.7 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 :separator option. For example, on Windows the file path separator is \:

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

2.8 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.9 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.10 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.