{"id":834,"date":"2026-11-19T21:41:07","date_gmt":"2026-11-19T13:41:07","guid":{"rendered":"http:\/\/www.tripatnews.com\/blog\/?p=834"},"modified":"2026-11-19T21:41:07","modified_gmt":"2026-11-19T13:41:07","slug":"how-to-convert-casing-in-ruby-4e32-303c7c","status":"publish","type":"post","link":"http:\/\/www.tripatnews.com\/blog\/2026\/11\/19\/how-to-convert-casing-in-ruby-4e32-303c7c\/","title":{"rendered":"How to convert casing in Ruby?"},"content":{"rendered":"<p>Hey folks, if you\u2019ve ever messed around with text formatting in Ruby\u2014say, 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\u2014you 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\u2014whether it\u2019s labeling product lines, coding our internal tools, or making sure our customer orders are logged the right way. Let\u2019s break down exactly how to convert casing in Ruby, no jargon, just real stuff I use daily. <a href=\"https:\/\/www.ranchengmachine.com\/drilling-accessories\/casing\/\">Casing<\/a><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.ranchengmachine.com\/uploads\/202326719\/small\/borehole-drilling-machine47cde6b4-abba-4600-8638-5fa8253fd44d.jpg\"><\/p>\n<p>First off, let\u2019s get on the same page: Ruby\u2019s got built-in methods for most basic casing, but I\u2019ve seen so many devs (and even old-timers who\u2019ve been coding Ruby for years) trip up on edge cases. Like, what if your string has hyphens, or it\u2019s mixed case, or you don\u2019t want Ruby to mangle acronyms? (Ever had \u201cAPI\u201d turn into \u201cApi\u201d because you used a basic method? Ugh, that\u2019s the worst.) That\u2019s where I come in\u2014between selling casings and debugging code snippets for our team, I\u2019ve sorted through all the gotchas, so you don\u2019t have to.<\/p>\n<p>Let\u2019s start with the basics. If you\u2019ve got a string and just want the simplest conversions, Ruby\u2019s core String class methods got you. No gems needed here\u2014this is vanilla Ruby, which is awesome.<\/p>\n<h3>camelCase to snake_case (and vice versa)<\/h3>\n<p>Camel case is all one word, capitalizing each new word like <code>userName<\/code> or <code>orderItem<\/code>. Snake case is underscores, like <code>user_name<\/code> or <code>order_item<\/code>. Our team uses snake case for variable names because it\u2019s easy to read in our codebase, but when we export data to spreadsheets for our manufacturing team, we switch to camel case.<\/p>\n<p>To turn camel case into snake case, I grab <code>snake_case<\/code>\u2014wait, no, it\u2019s actually <code>underscore<\/code>! Yeah, that\u2019s the one. Let\u2019s test it:<\/p>\n<pre><code class=\"language-ruby\">productLine = &quot;HighTempCasingModel&quot;\nputs productLine.underscore\n# Output: &quot;high_temp_casing_model&quot;\n<\/code><\/pre>\n<p>That\u2019s straightforward, but wait\u2014this only works if your camel case is \u201cUpperCamelCase\u201d or \u201clowerCamelCase\u201d (no acronyms, mostly). If you have something like \u201cAPIKey\u201d and run <code>underscore<\/code>, you\u2019ll get \u201capi_key\u201d which is fine, but sometimes I want it to stay \u201cAPIKey\u201d for product labeling. I\u2019ll touch on how to tweak that later.<\/p>\n<p>Now, snake case back to camel case. For lower camel case (starts with lowercase, like <code>highTempCasingModel<\/code>), there\u2019s <code>camelize(:lower)<\/code>. For upper camel case (starts with uppercase, like <code>HighTempCasingModel<\/code>), just <code>camelize<\/code>. Let\u2019s do that:<\/p>\n<pre><code class=\"language-ruby\">productCode = &quot;casing_part_123&quot;\nputs productCode.camelize\n# Output: &quot;CasingPart123&quot;\nputs productCode.camelize(:lower)\n# Output: &quot;casingPart123&quot;\n<\/code><\/pre>\n<p>Wait a second\u2014hold up. Those methods aren\u2019t 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\u2019s no big deal, but if you\u2019re using plain old Ruby, you\u2019ll need a quick workaround, or a tiny gem like <code>underscore<\/code> (which is super lightweight, no bloat). That\u2019s a pro tip I learned the hard way: don\u2019t get caught without knowing if a method is core or Rails-only.<\/p>\n<h3>Title Case, UPPERCASE, lowercase\u2014no fancy stuff needed<\/h3>\n<p>For the basics, Ruby\u2019s core methods work like a charm. Let\u2019s say you have input from a customer where someone wrote \u201cHIGHTEMP CASING MODEL\u201d in all caps, and you want to make it look nice in a report for our sales team.<\/p>\n<pre><code class=\"language-ruby\">customerNote = &quot;NEW ORDER FOR HIGH TEMP CASING MODEL 500&quot;\nputs customerNote.downcase\n# Output: &quot;new order for high temp casing model 500&quot;\nputs customerNote.upcase\n# Output: &quot;NEW ORDER FOR HIGH TEMP CASING MODEL 500&quot; (obviously)\nputs customerNote.capitalize\n# Wait, no\u2014capitalize only makes the first character uppercase, the rest lowercase. Oh right, so that gives &quot;New order for high temp casing model 500&quot;\n<\/code><\/pre>\n<p>If you want proper Title Case (each major word capitalized, no random lowercase), like \u201cNew Order for High Temp Casing Model 500\u201d, that\u2019s where a lot of people mess up. The core <code>titleize<\/code> method is again from Active Support, but if you\u2019re in plain Ruby, don\u2019t go writing a messy regex that breaks on words like \u201cfor\u201d or \u201cthe\u201d\u2014that\u2019s a waste of time. I use a small gem called <code>titleize<\/code> for plain Ruby, it\u2019s way easier. Install it with <code>gem install titleize<\/code>, then:<\/p>\n<pre><code class=\"language-ruby\">require &quot;titleize&quot;\nputs customerNote.titleize\n# Output: &quot;New Order for High Temp Casing Model 500&quot;\n<\/code><\/pre>\n<p>Perfect, that\u2019s exactly what our sales team wants on their order sheets.<\/p>\n<h3>The edge case I see everyone missing: acronyms and mixed characters<\/h3>\n<p>Here\u2019s where I come back to my casing supplier side. Our products have acronyms like \u201cIP\u201d (Ingress Protection) and \u201cUL\u201d (Underwriters Laboratories) in their names. If I run <code>titleize<\/code> on \u201cip67_casing\u201d, it turns into \u201cIp67 Casing\u201d and that\u2019s wrong\u2014we need it to stay \u201cIP67 Casing\u201d. Or if I have \u201cAPI_CASING\u201d and run <code>snake_case<\/code>, it becomes \u201capi_casing\u201d which is fine, but if I\u2019m labeling a product for export, I want \u201cAPI Casing\u201d not \u201cApi Casing\u201d.<\/p>\n<p>How do I fix that? Let\u2019s talk regex, but keep it simple\u2014no complicated patterns. For acronyms, I\u2019ll use a quick sub after running the basic method. For example, to fix \u201cApi\u201d back to \u201cAPI\u201d:<\/p>\n<pre><code class=\"language-ruby\">modelName = &quot;api_casing&quot;\nfixedName = modelName.camelize(:lower).sub(\/Api\/, 'API')\nputs fixedName\n# Output: &quot;apiCasing&quot; (wait, no, if I want the label to be &quot;API Casing&quot;, I\u2019d do:\nlabelName = modelName.camelize.split(\/(?=[A-Z])\/).join(' ').sub(\/Api\/, 'API')\nputs labelName\n# Output: &quot;API Casing&quot;\n<\/code><\/pre>\n<p>That split part splits the camel case string at every uppercase letter, so \u201capiCasing\u201d becomes \u201capi Casing\u201d, then I replace the lowercase \u201cApi\u201d with \u201cAPI\u201d. Easy, no magic. For uppercase acronyms in snake case, like \u201cUL94_casing\u201d, I can even write a helper method I use in our internal tools so I don\u2019t have to type that every time. Here\u2019s the helper I made:<\/p>\n<pre><code class=\"language-ruby\">def format_product_name(name)\n  # Turn snake case to human-readable, keep acronyms uppercase\n  name.split('_').map do |word|\n    # If the word is all uppercase (like UL, IP), leave it, else capitalize\n    word.match?(\/^[A-Z]+$\/) ? word : word.capitalize\n  end.join(' ')\nend\n\nputs format_product_name(&quot;ul94_high_temp_casing&quot;)\n# Output: &quot;UL94 High Temp Casing&quot;\n<\/code><\/pre>\n<p>That\u2019s 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\u2014snake, camel, even kebab case (which I know some of you use for URLs, like \u201chigh-temp-casing\u201d). Wait, kebab case? Yeah, that\u2019s another one. To turn snake case to kebab case, just use <code>gsub('_', '-')<\/code>:<\/p>\n<pre><code class=\"language-ruby\">urlFriendlyName = &quot;high_temp_casing_model_500&quot;.gsub('_', '-')\nputs urlFriendlyName\n# Output: &quot;high-temp-casing-model-500&quot;\n<\/code><\/pre>\n<p>Kinda obvious, but so many people forget that\u2019s how easy it is.<\/p>\n<h3>When to use gems vs core methods (from a supplier\u2019s perspective)<\/h3>\n<p>I can\u2019t stress this enough: vanilla Ruby is great for simple stuff. If you\u2019re just converting a quick string in a script, don\u2019t install a whole gem. But if you\u2019re building something for work\u2014like our admin tool that tracks casing inventory for different regions, each needing a different casing format (US vs EU has different labeling rules)\u2014gems are worth it. The <code>inflector<\/code> 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.<\/p>\n<p>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 \u201ccasing_model_\u00e9lectrique\u201d in French. Does <code>downcase<\/code> or <code>titleize<\/code> handle that? Yeah, Ruby\u2019s string methods are Unicode-aware, so that\u2019s 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\u2019s a huge relief when dealing with global orders.<\/p>\n<h3>Common mistakes I see (and how to avoid them)<\/h3>\n<p>Let\u2019s wrap this up with the stuff I\u2019ve messed up so many times:<\/p>\n<ol>\n<li><strong>Forgetting Active Support methods aren\u2019t core<\/strong>: If you try to run <code>camelize<\/code> in a plain Ruby script, you\u2019ll get a NoMethodError. Save yourself the headache\u2014use <code>underscore<\/code> gem for plain Ruby, or require <code>active_support\/core_ext\/string<\/code> if you\u2019re in Rails.<\/li>\n<li><strong>Mangling acronyms<\/strong>: Never use a basic method on strings with acronyms like \u201cAPI\u201d or \u201cIP\u201d without tweaking. That\u2019s how you end up with \u201cIp Casing\u201d on a product label, and trust me, the manufacturing team will yell at you.<\/li>\n<li><strong>Overcomplicating it<\/strong>: Don\u2019t 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.<\/li>\n<\/ol>\n<p>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\u2019s on both the coding and manufacturing sides (I sell these casings, I use the code that labels \u2019em), I know how important it is to get the casing right\u2014no misnamed parts, no broken links, no confused clients.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.ranchengmachine.com\/uploads\/26719\/small\/hydraulic-rige4dfc.jpg\"><\/p>\n<p>If you\u2019re dealing with product labels, inventory logs, or any text that needs clean, consistent casing for your casing orders\u2014let\u2019s chat. Whether you need help with a script that converts your order data or you\u2019re looking for reliable casings for your manufacturing line, my team\u2019s got you. Hit us up to talk through your needs, no pressure, no weird hoops.<\/p>\n<p><a href=\"https:\/\/www.ranchengmachine.com\/anchorage-device\/\">Anchorage Device<\/a> References<\/p>\n<ol>\n<li>Ruby Official String Methods Documentation<\/li>\n<li>Active Support Inflector Guide<\/li>\n<li>RubyGems: underscore Gem<\/li>\n<li>RubyGems: titleize Gem<\/li>\n<\/ol>\n<hr>\n<p><a href=\"https:\/\/www.ranchengmachine.com\/\">Henan Rancheng Machinery Co., Ltd.<\/a><br \/>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.<br \/>Address: No. 33 Jinsuo Road, High-tech Zone, Zhengzhou, Henan<br \/>E-mail: robin@ranchengmachine.com<br \/>WebSite: <a href=\"https:\/\/www.ranchengmachine.com\/\">https:\/\/www.ranchengmachine.com\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey folks, if you\u2019ve ever messed around with text formatting in Ruby\u2014say, needing to turn a &hellip; <a title=\"How to convert casing in Ruby?\" class=\"hm-read-more\" href=\"http:\/\/www.tripatnews.com\/blog\/2026\/11\/19\/how-to-convert-casing-in-ruby-4e32-303c7c\/\"><span class=\"screen-reader-text\">How to convert casing in Ruby?<\/span>Read more<\/a><\/p>\n","protected":false},"author":443,"featured_media":834,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[797],"class_list":["post-834","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-industry","tag-casing-4311-30edb1"],"_links":{"self":[{"href":"http:\/\/www.tripatnews.com\/blog\/wp-json\/wp\/v2\/posts\/834","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tripatnews.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tripatnews.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tripatnews.com\/blog\/wp-json\/wp\/v2\/users\/443"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tripatnews.com\/blog\/wp-json\/wp\/v2\/comments?post=834"}],"version-history":[{"count":0,"href":"http:\/\/www.tripatnews.com\/blog\/wp-json\/wp\/v2\/posts\/834\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.tripatnews.com\/blog\/wp-json\/wp\/v2\/posts\/834"}],"wp:attachment":[{"href":"http:\/\/www.tripatnews.com\/blog\/wp-json\/wp\/v2\/media?parent=834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tripatnews.com\/blog\/wp-json\/wp\/v2\/categories?post=834"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tripatnews.com\/blog\/wp-json\/wp\/v2\/tags?post=834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}