Лупайте сю скалу...

Іван Франко


 RSS | Atom

19 June 2015

TOP »

Selected projects See all »

Banalize: Bash static code analyzerDBIx::Report::ExcelShell settings, AKA dotfilesNagios Monitoring for Couchbase serverNagira: RESTful API for NagiosStartpack for Github pages developmentWeb iPhoto

Travel Blog Витрішки »

Україномовний блог Витрішки - блог про все, крім роботи. Цикл статей "Літо на півночі Японії" з цього блогу тепер опублікований у Витрішках

Computer Blog All posts »

iPhoto and files permission Is MacOSX 10.9 == MacOS 9.x? Running Chef roles from Capistrano und Capiche Github & Jekyll: Speeding up Jekyll siteGithub & Jekyll: More experience with Jekyll and setup changesBuild new hosts with Capistrano and Chef Request Tracker: More about custom CSS for RT4Ruby 2 test drive Nagira v0.2.5 release It's UNIX my dear Watson

Slides See all »

Nagira @TLUG, 2012Rspec and Guard

About

Ярило Yarylo, Jarilo me @ github me @ CPAN Dmytro (CV)

Memorizable password generation

I was in need to write function to generate pseudo-random strings that are easier to remember and type for a human. To use either for passwords or let's say random coupon codes.

In my search I came across this solution. It sure works, but does not look pretty enough.

def self.random_password
  c = %w( b c d f g h j k l m n p qu r s t v w x z ) +
      %w( ch cr fr nd ng nk nt ph pr rd sh sl sp st th tr )
  v = %w( a e i o u y )
  f, r = true, ''
  6.times do
    r << ( f ? c[ rand * c.size ] : v[ rand * v.size ] )
    f = !f
  end
  2.times do
    r << ( rand( 9 ) + 1 ).to_s
  end
  r
end

So I decided to do my own. After few modifications I came up with example below:

class Array
  def rand
    self[Random.rand(self.size)]
  end
end

def random_password size=4
    c = %w(b c d f g h j k l m n p qu r s t v w x z ch cr fr nd ng nk nt ph pr rd sh sl sp st th tr)
    v = %w(a e i o u y)
    (0...size).reduce('') { |r| r << c.rand << v.rand }
end

All together it's not really any shorter, but function itself is less than half the size of the original, and as a side effect, there's also Array.rand method.






дмитро ковальов
dmytro @ github
dmytro.sytes.net