Ozimodo Madness

Well, defunkt and I were working on the oz script, and we found a little bug, which he fixed like this (I thought this was pretty slick)


irb(main):058:0> host = "modzer0.cs.uaf.edu"
=> "modzer0.cs.uaf.edu"
irb(main):059:0>  domain = host =~ /([^\/]+)\/(.+)/ ? "#{$1}#{':80'}/#{$2}" : "#{host}#{':80'}"
=> "modzer0.cs.uaf.edu:80"
irb(main):060:0> host = "modzer0.cs.uaf.edu/hank-typo"
=> "modzer0.cs.uaf.edu/hank-typo"
irb(main):061:0>  domain = host =~ /([^\/]+)\/(.+)/ ? "#{$1}#{':80'}/#{$2}" : "#{host}#{':80'}"
=> "modzer0.cs.uaf.edu:80/hank-typo"

I need to make sure to brush up on my RegExp Skillz before I get into the real world.



Comments

  1. defunkt

    July 10, 2006 at 11:39 PM

    This can be done with URI, too:

    
    >> require 'uri'
    => true
    >> uri = URI.parse 'http://modzer0.cs.uaf.edu/hank-typo'
    => #
    >> "#{uri.host}:#{uri.port}:#{uri.path}"
    => "modzer0.cs.uaf.edu:80:/hank-typo"
    

    Not sure if that’s easier or not. Regexps for life.