Secure random strings in Ruby

File this under “Well-documented features I never noticed before.” In Ruby, if you need to generate random strings for various purposes, there’s a native module: SecureRandom.

In Ruby 1.8, you can generate Base-64-encoded strings, hex, random numbers (integers), and random bytes. Ruby 1.9 adds the ability to generate UUIDs and URL-safe Base64 strings. Really simple, and not particularly interesting — unless, like me, you found yourself thinking you had to write this functionality yourself.