Tuesday, April 07, 2009

Rails - Read More, Code Less, do Peer Reviews

For some previous projects I rolled some simple code for generating a random string - used to generate a password reset token for example:

class StringUtils
def self.generate_random(length,
chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
ret = ''
ret += chars.at(rand(chars.length)) while ret.length < length
ret
end
end
view raw gistfile1.rb hosted with ❤ by GitHub


Now I've found that this type of functionality has been in Rails for some time - now as ActiveSupport::SecureRandom and previously as Rails::SecretKeyGenerator.

Ok so that is simple code and probably isn't a problem but the lesson here is: read more, code less, do peer reviews...don't waste a lot of time re-inventing the wheel.