Random Ruby Password Magic

I have never been a big language geek. My CV lists a raft of them; C#, JavaScript, Ruby, VBScript and even COBOL. But I know many coders (like Kenny, Brian or David) who have a much deeper understanding than I do. On a daily basis Brian shows me twists and turns of C# that I had no idea about. I am not ashamed about this though. Programmers aren’t one type. We have generalists and specialists, high-level chaps and on-the-metal sorts.

Occasionally though along comes a language or system that works the way I think. I then go to another level of understanding with it. HTML and CSS may not be strict programming languages but I do know them to a far greater degree than I do other languages. I am dead confident with them which is a nice feeling.

I reckon Ruby is fast revealing itself to be a language I will grow to be confident in, one I’ll understand beyond my usual level. It feels right.

Just an hour ago I needed some Ruby code that would generate a random string of characters (for a password system in this case.) One Google later and I came across this post. A comment by Scott Becker blew me away with its usesfulness and simplicity. Here is the code:
schars = "0124356789abcdefghijk"
password = ""
1.upto(8) { password += schars[rand(schars.length),1] }

In other languages a line like that would leave me scratching my head and turning to Brian but in Ruby it made elegant, simple sense.

Viewing 4 Comments

Trackbacks

close Reblog this comment
blog comments powered by Disqus