Generate Unique ID
June 23, 2008
Here is a very simple way to generate a unique id.
[Background support : Abhishek S.]
def generate_unique_id( len )
chars_pattern = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
unique_id = ""
1.upto(len) {
|i| unique_id << chars_pattern[rand(chars_pattern.size-1)] }
return unique_id
end
#generates a key of length 10
mykey = generate_unique_id(10)
print mykey # gives a result like "qeKX0myIQh"
Entry Filed under: RoR, coding. Tags: generate, id, key, Rails, RoR, Ruby, security, unique.
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed