Get current timestamp in Ruby On Rails and use it to create a random alphanumeric string

# Get current time stamp
dateTime = Time.new
timestamp = dateTime.to_time.to_i
# This allows you to do String.random_alphanumeric(num) to get a random string that is num characters long. Useful for generating random identifier keys. ''This allows you to do String.random_alphanumeric(num) to get a random string that is num characters long. Useful for generating random identifier keys.

alphanumerics = [('0'..'9'),('A'..'Z'),('a'..'z'),(timestamp)].map {|range| range.to_a}.flatten
member_card_no = (0...16).map { alphanumerics[Kernel.rand(alphanumerics.size)] }.join

# Check generated alphanumeric string
puts member_card_no

No comments

Powered by Blogger.