Ruby String#Nameize Revised

This morning, Kevin Glowacz (@kevinglowacz) replied to me a few times on Twitter about Ruby String#Nameize class extension I had posted a while back.  I had done some work to it after posting it here.  Kevin also asked me a few questions about oddities that were in it that have since been resolved.  So thanks to his prodding, you get a slightly updated version…

The only real “feature” is that it will now handle full names just fine. Otherwise, the rest of the stuff was mostly performance related.  Here it is:

class String
  # Extension of the string class to properly handle camel names
  def nameize
    if self.match(/ /)
      # If the name has a space in it, we gotta run the parts through the nameizer.
      name = self.split(' ').each { |part| part.nameize! }.join(' ')
      return name
    elsif self.match(/^[A-Z]/)
      # If they took the time to capitalize their name then let's just jump out.
      return self
    else
      # If there are no spaces and there is no prior
      # capitalization then let's downcase the whole thing.
      name = self.downcase
    end
    # Let's now assume that they were lazy...
    return case
    when name.match(/^mac/)
      name.gsub(/^mac/, "").capitalize.insert(0, "Mac")
    when name.match(/^mc/)
      name.gsub(/^mc/, "").capitalize.insert(0, "Mc")
    when name.match(/^o\'/)
      name.split("'").each{ |piece| piece.capitalize! }.join("'")
    else
      name.capitalize # name is a first name or it's not Irish then capitalize it.
    end
  end

  def nameize!
    replace nameize # BANG!
  end

end

As always – question, comments, suggestions – shoot me an email, leave a comment, or hit me on Twitter (@PatrickTulskie).

Posted in Code, Software Development, ruby at December 30th, 2008. Trackback URI: trackback
Tags: , , , , , , , , , ,

5 Responses to “Ruby String#Nameize Revised”

  1. January 21st, 2009 at 7:39 pm #Clinton Skakun

    Can’t wait to get into Ruby! Looks awesome.

    Currently, I use PHP for application development. Instead of using Ruby for that I’m going to use it for building programs with GUIs. I’m looking into building in GTK and Qt. GTK because it’s entirely free and Qt because it’s the best, even though the license costs money. Never know I might buy it, but not until I’m made some real money on software development.

    Anyways I think it’s great that you’re into Ruby.

    Have a great one!
    Clinton Skakun

  2. February 7th, 2009 at 3:46 am #Oz

    Hey Patrick, sorry that this comment isn’t really about the post, but I just saw this site and I really like it! Nice layout and everything. I enjoy your writing style too

  3. August 28th, 2009 at 2:46 am #Brookr

    Thanks for sharing this, just what I needed!

  4. September 10th, 2009 at 12:14 pm #raggi

    I can see where you’re trying to apply “performance optimizations” but they’re introducing bugs, that you don’t seem to have benchmark, profile, or test coverage for.

    You don’t need gsub with a regex of /^ … /

    You could do the entire sub line operation in a single sub, and probably should do. Sub isn’t delete, afterall. Oh, and the two sub cases are equivalent, with a different first group.

    Stop using String#match without utilising the match data, and for that matter, use case properly, rather than this new way you’ve learned.

    Oh screw it… here comes codes…



    class String
    SPACE = ' '
    APOS = "'"

    # Extension of the string class to properly handle camel names
    def nameize
    case self
    when / /
    # If the name has a space in it, we gotta run the parts through the nameizer.
    split(SPACE).each { |part| part.nameize! }.join(SPACE)
    when /^[A-Z]/
    # If they took the time to capitalize their name then let's just jump out.
    self
    when /^(mac|mc)(\w)(.*)$/i
    "#{$1.capitalize}#{$2.capitalize}#{$3}"
    when /^o\'/i
    split(APOS).each{ |piece| piece.capitalize! }.join(APOS)
    else
    capitalize # Basically if the name is a first name or it's not Irish then capitalize it.
    end
    end

    def nameize!
    replace nameize # BANG!
    end

    end

    require 'rubygems'
    require 'minitest/unit'
    include MiniTest::Assertions
    assert_equal("Martin O'Donald", "martin o'donald".nameize)
    assert_equal("Randy McFarley", "Randy mcfarley".nameize)
    assert_equal("Sue MacDonald", "sue macdonald".nameize)

    Have fun, and play safe.

  5. September 20th, 2009 at 7:42 pm #Patrick Tulskie

    Thanks for that raggi. Great set of tweaks. Yeah I’ve come to learn a great deal since I originally wrote this dirty code. Case has been one of those things I’ve stopped abusing such as you saw before. I’m glad that people are still finding this useful and improving upon it. As you’ll probably notice, I’ve stopped writing on my blog as much and have been reading through the code of others’ as of late.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>