Generate Unique ID

June 23, 2008 at 11:47 am Leave a comment

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: coding, RoR. Tags: , , , , , , , .

code optimization Regular expression

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Categories

posts[:recent]

@@name = PRAYAS

This blog has more intention for my self reference@24×7. But if some search engine crawler directs you to my page, believe I may learn form your comments or feedback. Hence feel free to comment good or bad!
prayas2010@gmail.com

visitors[:since_Mar'08]

free web counter

Prayas@Nikon

A colored life

Chhweet Kidss

An independent world

More Photos

Feeds


Follow

Get every new post delivered to your Inbox.