Skip to content

Instantly share code, notes, and snippets.

@momota
Last active June 14, 2016 06:13
Show Gist options
  • Save momota/4780232 to your computer and use it in GitHub Desktop.
Save momota/4780232 to your computer and use it in GitHub Desktop.
ランダムな文字列生成用。一時的なパスワードなど。
# conding: utf-8
def random_string( length )
str = ('a'..'z').to_a + ('A'..'Z').to_a +
('0'..'9').to_a + ['-', '+', '_', '@', '=', '&', '$', '#', '?']
Array.new( length ){ str[rand( str.size )] }.join
end
# -------------------------------------------------------------------
# main
if __FILE__ == $0
length = 15 # default length
length = ARGV[0].to_i if ARGV[0].to_i != 0
puts random_string( length )
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment