Generates random alphanumeric characters.
Rand62.safe(10)
=> "yTX35RzROS"
Rand62 has better space efficiency than SecureRandom.uuid
or SecureRandom.hex
. It's sexier than SecureRandom.base64
or SecureRandom.urlsafe_base64
as it doesn't contain any symbols.
If you care more about database efficiency than Ruby, and user-friendliness of the look of IDs, use Rand62.
Add this line to your application's Gemfile:
gem 'rand62'
And then execute:
$ bundle
Or install it yourself as:
$ gem install rand62
There are two methods: safe
and fast
. Pass the length of generated string to the method.
require 'rand62'
Rand62.safe(10)
=> "yTX35RzROS"
Rand62.fast(10)
=> "sWCGqxY2kF"
It's recommended to use safe
until the performance becomes a real problem.
The following test results came from ruby 1.9.3p125 on iMac 2011 Core i5 2.7GHz.
1000.times{ SecureRandom.urlsafe_base64 } # 0.006427
1000.times{ Rand62.fast(22) } # 0.015881
1000.times{ Rand62.safe(22) } # 0.161878
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request