Wednesday, April 21, 2010

ruby fail

Don't do this:

#/usr/bin/ruby
arr = Array.new
something = "hello world"
arr.push(something)
something = "whoops"
puts arr #sends 'whoops' to stdout

Found this out while working on a commandline utility that corrects spelling errors. I was looping through the characters in a string, potentially altering the word we were given and then pushing the result into an array that held the values we might want to return (I was going to post-process them as well).

Implementing a deep copy in ruby didn't strike me as a good use of time, so I returned the first 'correct' value instead of returning an array of possible corrections. I will probably post this to my github later, for posterity.

No comments: