Articles Feed

Authors

Categories

Want clean code? Stop using the editor.

by: eric | December 21st, 2007 | 3 comments »

Recently I was adding some accessors to a ruby class on the fly, and I kept getting confused by this block:

eval <<-eof def>

The code isn’t particularly complicated, just creating a quick accessor to get at the underlying hash. Yet I had a couple bugs simply because without the editor to give me hints, the code was that much harder to follow. This turns out to be a blessing in disguise, as I am forced to write extremely clean code just to keep from confusing myself. Opening up the String class cleans this up a bit, and removes duplication:

eval <<-eof def>

There much better. So the next time you think your code isn’t as clear as it could be, or maybe if you think it’s perfect, try reading it without any syntax highlighting. If you can still follow it quickly and easily, then maybe it doesn’t entirely suck.

Unrubify

by: paul | December 19th, 2007 | 2 comments »

Often times while writing meta programming code, I am using the eval function and doing manipulation on method/class/variable names. Today I needed to unrubify a name. I haven’t seen this done regularly, and unfortunately there is no fun rails method to do it (there is one for rubifying a string). So, here is my attempt at it, for anyone trying to solve the same problem.

def unrubify(sentence)  
  sentence.capitalize!
  sentence.gsub!(/_(.)/) { $1.upcase }
  return sentence 
end  

Any suggestions on improvements please let me know!