Posted in

How to convert casing in Ruby?

Hey folks, if you’ve ever messed around with text formatting in Ruby—say, needing to turn a snake_case variable into a proper Title Case for a UI label, or a UPPERCASE input from a form into something that looks clean in a report—you know how easy (and how tricky) it can be to mix up edge cases. As a casing supplier, I work with text all the time—whether it’s labeling product lines, coding our internal tools, or making sure our customer orders are logged the right way. Let’s break down exactly how to convert casing in Ruby, no jargon, just real stuff I use daily. Casing

First off, let’s get on the same page: Ruby’s got built-in methods for most basic casing, but I’ve seen so many devs (and even old-timers who’ve been coding Ruby for years) trip up on edge cases. Like, what if your string has hyphens, or it’s mixed case, or you don’t want Ruby to mangle acronyms? (Ever had “API” turn into “Api” because you used a basic method? Ugh, that’s the worst.) That’s where I come in—between selling casings and debugging code snippets for our team, I’ve sorted through all the gotchas, so you don’t have to.

Let’s start with the basics. If you’ve got a string and just want the simplest conversions, Ruby’s core String class methods got you. No gems needed here—this is vanilla Ruby, which is awesome.

camelCase to snake_case (and vice versa)

Camel case is all one word, capitalizing each new word like userName or orderItem. Snake case is underscores, like user_name or order_item. Our team uses snake case for variable names because it’s easy to read in our codebase, but when we export data to spreadsheets for our manufacturing team, we switch to camel case.

To turn camel case into snake case, I grab snake_case—wait, no, it’s actually underscore! Yeah, that’s the one. Let’s test it:

productLine = "HighTempCasingModel"
puts productLine.underscore
# Output: "high_temp_casing_model"

That’s straightforward, but wait—this only works if your camel case is “UpperCamelCase” or “lowerCamelCase” (no acronyms, mostly). If you have something like “APIKey” and run underscore, you’ll get “api_key” which is fine, but sometimes I want it to stay “APIKey” for product labeling. I’ll touch on how to tweak that later.

Now, snake case back to camel case. For lower camel case (starts with lowercase, like highTempCasingModel), there’s camelize(:lower). For upper camel case (starts with uppercase, like HighTempCasingModel), just camelize. Let’s do that:

productCode = "casing_part_123"
puts productCode.camelize
# Output: "CasingPart123"
puts productCode.camelize(:lower)
# Output: "casingPart123"

Wait a second—hold up. Those methods aren’t in vanilla Ruby. Oh right! They come from Active Support, which is part of Rails. I use Rails for our internal admin tool to track casing inventory, so that’s no big deal, but if you’re using plain old Ruby, you’ll need a quick workaround, or a tiny gem like underscore (which is super lightweight, no bloat). That’s a pro tip I learned the hard way: don’t get caught without knowing if a method is core or Rails-only.

Title Case, UPPERCASE, lowercase—no fancy stuff needed

For the basics, Ruby’s core methods work like a charm. Let’s say you have input from a customer where someone wrote “HIGHTEMP CASING MODEL” in all caps, and you want to make it look nice in a report for our sales team.

customerNote = "NEW ORDER FOR HIGH TEMP CASING MODEL 500"
puts customerNote.downcase
# Output: "new order for high temp casing model 500"
puts customerNote.upcase
# Output: "NEW ORDER FOR HIGH TEMP CASING MODEL 500" (obviously)
puts customerNote.capitalize
# Wait, no—capitalize only makes the first character uppercase, the rest lowercase. Oh right, so that gives "New order for high temp casing model 500"

If you want proper Title Case (each major word capitalized, no random lowercase), like “New Order for High Temp Casing Model 500”, that’s where a lot of people mess up. The core titleize method is again from Active Support, but if you’re in plain Ruby, don’t go writing a messy regex that breaks on words like “for” or “the”—that’s a waste of time. I use a small gem called titleize for plain Ruby, it’s way easier. Install it with gem install titleize, then:

require "titleize"
puts customerNote.titleize
# Output: "New Order for High Temp Casing Model 500"

Perfect, that’s exactly what our sales team wants on their order sheets.

The edge case I see everyone missing: acronyms and mixed characters

Here’s where I come back to my casing supplier side. Our products have acronyms like “IP” (Ingress Protection) and “UL” (Underwriters Laboratories) in their names. If I run titleize on “ip67_casing”, it turns into “Ip67 Casing” and that’s wrong—we need it to stay “IP67 Casing”. Or if I have “API_CASING” and run snake_case, it becomes “api_casing” which is fine, but if I’m labeling a product for export, I want “API Casing” not “Api Casing”.

How do I fix that? Let’s talk regex, but keep it simple—no complicated patterns. For acronyms, I’ll use a quick sub after running the basic method. For example, to fix “Api” back to “API”:

modelName = "api_casing"
fixedName = modelName.camelize(:lower).sub(/Api/, 'API')
puts fixedName
# Output: "apiCasing" (wait, no, if I want the label to be "API Casing", I’d do:
labelName = modelName.camelize.split(/(?=[A-Z])/).join(' ').sub(/Api/, 'API')
puts labelName
# Output: "API Casing"

That split part splits the camel case string at every uppercase letter, so “apiCasing” becomes “api Casing”, then I replace the lowercase “Api” with “API”. Easy, no magic. For uppercase acronyms in snake case, like “UL94_casing”, I can even write a helper method I use in our internal tools so I don’t have to type that every time. Here’s the helper I made:

def format_product_name(name)
  # Turn snake case to human-readable, keep acronyms uppercase
  name.split('_').map do |word|
    # If the word is all uppercase (like UL, IP), leave it, else capitalize
    word.match?(/^[A-Z]+$/) ? word : word.capitalize
  end.join(' ')
end

puts format_product_name("ul94_high_temp_casing")
# Output: "UL94 High Temp Casing"

That’s game-changer for our team. No more mislabeled products, no more time spent fixing names in spreadsheets. I test this with every casing format we use—snake, camel, even kebab case (which I know some of you use for URLs, like “high-temp-casing”). Wait, kebab case? Yeah, that’s another one. To turn snake case to kebab case, just use gsub('_', '-'):

urlFriendlyName = "high_temp_casing_model_500".gsub('_', '-')
puts urlFriendlyName
# Output: "high-temp-casing-model-500"

Kinda obvious, but so many people forget that’s how easy it is.

When to use gems vs core methods (from a supplier’s perspective)

I can’t stress this enough: vanilla Ruby is great for simple stuff. If you’re just converting a quick string in a script, don’t install a whole gem. But if you’re building something for work—like our admin tool that tracks casing inventory for different regions, each needing a different casing format (US vs EU has different labeling rules)—gems are worth it. The inflector gem (which is what Active Support uses, by the way) is super lightweight, only adds a few KB to your app, and handles all the casing stuff without messy regex. I use it for everything now, even our side project tools.

Wait, another thing: international casing? We sell casings to companies in Europe and Asia, so their product names have accents sometimes. Like, a product named “casing_model_électrique” in French. Does downcase or titleize handle that? Yeah, Ruby’s string methods are Unicode-aware, so that’s no problem. I tested last year with a product name from a Japanese client, the kanji and romaji casing converted perfectly, no weird characters breaking. That’s a huge relief when dealing with global orders.

Common mistakes I see (and how to avoid them)

Let’s wrap this up with the stuff I’ve messed up so many times:

  1. Forgetting Active Support methods aren’t core: If you try to run camelize in a plain Ruby script, you’ll get a NoMethodError. Save yourself the headache—use underscore gem for plain Ruby, or require active_support/core_ext/string if you’re in Rails.
  2. Mangling acronyms: Never use a basic method on strings with acronyms like “API” or “IP” without tweaking. That’s how you end up with “Ip Casing” on a product label, and trust me, the manufacturing team will yell at you.
  3. Overcomplicating it: Don’t write a custom regex for every casing format unless you have to. The built-in methods and small gems do 90% of the work, and the extra 10% is just a quick helper method like the one I showed for acronyms.

At the end of the day, casing conversion in Ruby is straightforward once you know where the methods live and what edge cases to watch for. As someone who’s on both the coding and manufacturing sides (I sell these casings, I use the code that labels ’em), I know how important it is to get the casing right—no misnamed parts, no broken links, no confused clients.

If you’re dealing with product labels, inventory logs, or any text that needs clean, consistent casing for your casing orders—let’s chat. Whether you need help with a script that converts your order data or you’re looking for reliable casings for your manufacturing line, my team’s got you. Hit us up to talk through your needs, no pressure, no weird hoops.

Anchorage Device References

  1. Ruby Official String Methods Documentation
  2. Active Support Inflector Guide
  3. RubyGems: underscore Gem
  4. RubyGems: titleize Gem

Henan Rancheng Machinery Co., Ltd.
Henan Rancheng Machinery Co., Ltd. is well-known as one of the leading casing manufacturers and suppliers in China, featured by quality products and competitive price. Please feel free to buy cost-efficient casing for sale here from our factory.
Address: No. 33 Jinsuo Road, High-tech Zone, Zhengzhou, Henan
E-mail: robin@ranchengmachine.com
WebSite: https://www.ranchengmachine.com/