Swizec Teller - a geek with a hatswizec.com

Senior Mindset Book

Get promoted, earn a bigger salary, work for top companies

Senior Engineer Mindset cover
Learn more

    Always look for the gem

    You can be doing something straightforward. Say, listing some items from the database and giving them an id so javascript can reference them. Because you know names are unique, you decide to use those for id's.

    You're using them for URL's too, so it makes sense. Simplifies Backbone routing code.

    At this point experienced developers get the heebie jeebies. A tiny little voice screaming "Watch out!" in the back of their head.

    I couldn't remember why using name felt funny, so I ignored the little voice and barged on. I deployed.

    Bam. Didn't work.

    After hours of debugging I finally traced the issue down to spaces in item names. Turns out id="a word" doesn't work. Who'd've thought? (hint: everyone)

    But that's an easy fix right? Just do something like:

    id="<%= item.name.gsub(' ', '-') %>"

    Or add a function to the model. Whatever. The point is, all you have to do is replace spaces with dashes. Simple.

    But you're wrong. You are creating a slug. It looks easy, but is actually very complicated. Luckily others have solved the problem already. Better than you.

    A quick Google search reveals a bunch of solutions do in fact exist, but Stringex comes out as the best solution for Ruby. There should be plenty for any language you love.

    Why spend an hour looking for and learning how to use a gem instead of solving the problem in two minutes of coding?

    Because -> from Stringex's documentation:

    # A simple prelude
    "simple English".to_url => "simple-english"
    "it's nothing at all".to_url => "its-nothing-at-all"
    "rock & roll".to_url => "rock-and-roll"
    # Let's show off
    "$12 worth of Ruby power".to_url => "12-dollars-worth-of-ruby-power"
    "10% off if you act now".to_url => "10-percent-off-if-you-act-now"
    # You don't even wanna trust Iconv for this next part
    "kick it en Français".to_url => "kick-it-en-francais"
    "rock it Español style".to_url => "rock-it-espanol-style"
    "tell your readers 你好".to_url => "tell-your-readers-ni-hao"

    Impressive to say the least.

    There's the simple space to dash conversion, but Stringex handles various symbols and common substrings as well. Converting $12 to 12-dollars strikes me as something particularly useful I'd never come up with.

    Truth be told, the most advanced slug generation function I've ever come up with (and I've written many) only stripped non-alphanumeric characters from the string before changing spaces to dashes.

    It sucked.

    When you find yourself solving a simple problem, look for a gem. Focus your attention to the problem at hand and let others take care of the externalities. You won't be sorry.

    Enhanced by Zemanta
    Published on July 23rd, 2013 in Programming, Ruby, Tools, Uncategorized, Web Design and Development

    Did you enjoy this article?

    Continue reading about Always look for the gem

    Semantically similar articles hand-picked by GPT-4

    Senior Mindset Book

    Get promoted, earn a bigger salary, work for top companies

    Learn more

    Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.

    Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.

    Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help 👉 swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.

    Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers 👉 ServerlessHandbook.dev

    Want to Stop copy pasting D3 examples and create data visualizations of your own? Learn how to build scalable dataviz React components your whole team can understand with React for Data Visualization

    Want to get my best emails on JavaScript, React, Serverless, Fullstack Web, or Indie Hacking? Check out swizec.com/collections

    Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog

    Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com

    By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️

    Created by Swizec with ❤️