Jan 03, 2008 @ 09:33 am

It’s been a while since the last edition of Rubyize this. I have waited all this time because I like when people become impatient. I have received 254 939 emails from desperate people asking me : “When will you post your 5th edition?”, “Is it hard to be that excellent?”, “You rock Frank. Warm kisses… - Vanessa” and such.

Hold on a second, gotta update my checklist :

  1. Invent myself a pleasing reality
  2. Impress people with fictitious numbers
  3. Write a bad intro for the 5th edition

2 days ago I received an email from Scott Patten, an active web developer based in Vancouver. He is currently helping to organize RubyCamp Vancouver 2008, which is a free one-day gathering for Rubyists and Railers. In his email, he asked me if he could use the Rubyize this concept for an (un)conference event that would be held at RubyCamp. Of course, I said yes! It now seems official : Rubyize this will be part of RubyCamp Vancouver 2008! I was also happy when I read the following : “the audience will submit their refactorings to Refactor My Code“. That’s good for you, Marc!

Important : The future of Rubyize this

I need your help to create ugly pieces of code! I don’t want to become repetitive from edition to edition so I’d really appreciate if you could send me some fresh ideas. Just send me an email to frank@rubyfleebie.com containing an ugly piece of code as well as a small intro to help us understand what this is all about. I’ll make sure to put your name as well as a link to your website (if any) in the appropriate Rubyize This edition.

Enough babbling :

Tom wrote a very simple ruby script to highlight some words in a text. He chose to highlight the words with asterisks, like *that* (In 2 months, perhaps Tom will have to use his script to produce HTML output or something else… he will be screwed with those basic asterisks). Tom has absolutely no ruby background, help him refactor his code ala Ruby, that is : short, clean and simple.

By the way, Tom didn’t put a lot of effort in his algorithm. His dumb gsub thing will mistakenly replace parts of words instead of whole words only. Maybe some improvements would be needed there.

  1. @wordsToHighlight=["important","monkey","dancing"]
  2. def highlightText(input)
  3.   for i in 0..@wordsToHighlight.length-1 do
  4.     input = input.gsub(@wordsToHighlight[i],"*" + @wordsToHighlight[i] + "*")
  5.   end
  6.   return input
  7. end

Refactor this!

Bookmark this post : These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DZone
  • Reddit
  • del.icio.us
  • Digg
  • Furl
  • Technorati
  • StumbleUpon
Rate this post :
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...
Posted under : short & sweet
20 Comments
MyAvatars 0.2

wow! that’s awesome! glad to see your concept getting more and more traction and thx for promoting RmC w/ it, you’re the best.
Can’t wait for the t-shirt “Rubyize-me Frank”, when is it shipping already?

Comment by : macournoyer
— January 3, 2008 @ 10:18 am

MyAvatars 0.2

Rubyize this : 5th edition…

Here’s my take, it’s short but not perfect…

— January 3, 2008 @ 10:27 am

MyAvatars 0.2

Rubyize this : 5th edition…

— January 3, 2008 @ 10:33 am

MyAvatars 0.2

Rubyize this : 5th edition…

this dumbass ajax form is not working!…

— January 3, 2008 @ 10:34 am

MyAvatars 0.2

Rubyize this : 5th edition…

Revision 2, this time passing in list of words to highlight as a function argument like macournoyer did (first reply), which is much better than accessing a named variable outside of the function….

— January 3, 2008 @ 10:39 am

MyAvatars 0.2

Rubyize this : 5th edition…

Sorry about the form not working Jason, should be fixed now…

— January 3, 2008 @ 10:41 am

MyAvatars 0.2

Rubyize this : 5th edition…

Good solutions above. This is overkill, but can’t think of anything else that hasn’t already been done….

— January 3, 2008 @ 11:15 am

MyAvatars 0.2

Congrats for the RubyCamp Vancouver Frank! It’s nice to see that your work is appreciated over the whole country…

Comment by : Dan Simard
— January 3, 2008 @ 11:23 am

MyAvatars 0.2

Rubyize this : 5th edition…

i purposely wrote this without looking at the refactorings… funny how close it still is but it’s a bit different. a little more verbose imo but extensible. the only issue i have with it is that you are limited to one a single delimiter….

— January 3, 2008 @ 1:24 pm

MyAvatars 0.2

Rubyize this : 5th edition…

I’m sure the String#pad (not sure about the name either) utility function added here is available somewhere, but anyways. Also using the block version of gsub this time….

— January 3, 2008 @ 4:05 pm

MyAvatars 0.2

Wow, thanks all for your contributions… some pretty good stuff here! It’s always fun and instructive to watch how people solve problems in ruby.

@Marc,

Thanks! About the rubyize-this shirts, I’m starting to think about it seriously :)

Comment by : Frank
— January 3, 2008 @ 6:52 pm

MyAvatars 0.2

Rubyize this : 5th edition…

There are already better versions here…

— January 6, 2008 @ 10:27 am

MyAvatars 0.2

Rubyize this : 5th edition…

Nothing major here, just made it case insensitive. An issue I have is that hyphenated words like “monkey-wrench” will still get highlighted and I have no idea how to get the regexp to ignore words beginning or ending with a -….

— January 7, 2008 @ 9:51 am

MyAvatars 0.2

Rubyize this : 5th edition…

Probably went overboard….

— January 9, 2008 @ 12:55 am

MyAvatars 0.2

Rubyize this : 5th edition…

another one, neither of these work on contractions, this one features recursion and only involves modifying string.

This one outputs:
*hello* world, what’s *up*?

— January 9, 2008 @ 1:07 am

MyAvatars 0.2

Rubyize this : 5th edition…

found a bug in that one, didnt work when you had a full sentence and passed in a mark.

This one will correctly output +hello+ world, what’s +up+?

I’m going to stop now, sorry for the triple post….

— January 9, 2008 @ 1:11 am

MyAvatars 0.2

@Mike, I like the fact that you used recursion. It’s an interesting and clever approach. No worry about the triple post :)

Comment by : Frank
— January 9, 2008 @ 7:51 am

MyAvatars 0.2

Rubyize this : 5th edition…

How i did it….

— January 9, 2008 @ 7:43 pm

MyAvatars 0.2

Rubyize this : 5th edition…

Regexp.union is your friend! This also demonstrates a typical use of the splat operator to turn an array into an argument list, and the usage of back-references in global substitutions. One thing that I found recently is that your replacement string …

— January 26, 2008 @ 2:11 pm

MyAvatars 0.2

Rubyize this : 5th edition…

Now having read some of the others, I’ll borrow from Simon’s idea:…

— January 26, 2008 @ 2:16 pm




Leave a comment
Name (required)
Email (will not be publish) (required)
Website