码迷,mamicode.com
首页 > 其他好文 > 详细

ruby随机生成字符串

时间:2018-01-03 14:08:49      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:随机   字符   body   blog   and   一个   class   ret   ruby   

随机生成一个固定位数的字符串:

def newpass( len )
    chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
    newpass = ""
    1.upto(len) { |i| newpass << chars[rand(chars.size-1)] }
    return newpass
end
puts   newpass(15)

 

ruby随机生成字符串

标签:随机   字符   body   blog   and   一个   class   ret   ruby   

原文地址:https://www.cnblogs.com/xiaoff/p/8183077.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!