Swizec Teller - a geek with a hatswizec.com

    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

    Did you enjoy this article?

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

    Learned something new?
    Want to become an expert?

    Here's how it works 👇

    Leave your email and I'll send you thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.

    Join Swizec's Newsletter

    And get thoughtful letters 💌 on mindsets, tactics, and technical skills for your career. Real lessons from building production software. No bullshit.

    "Man, love your simple writing! Yours is the only newsletter I open and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"

    ~ Ashish Kumar

    Join over 14,000 engineers just like you already improving their careers with my letters, workshops, courses, and talks. ✌️

    Have a burning question that you think I can answer? I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.

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

    Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.

    Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first 💰 on the side with ServerlessReact.Dev

    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 bySwizecwith ❤️