Ruby193で乱数字生成
こんな方法があったのか!と驚いたのでメモ
- 配列
[*0..9, *'a'..'z', *'A'..'Z'].sample(10).join #これでも出来る [*0..9, *'a'..'z', *'A'..'Z'].sample(10) * "" #=> "eBoO8LnmIT"
- securerandom
require 'securerandom' SecureRandom.base64[0..9] #=> "Bm00Gza1eJ"
こんな方法があったのか!と驚いたのでメモ
[*0..9, *'a'..'z', *'A'..'Z'].sample(10).join #これでも出来る [*0..9, *'a'..'z', *'A'..'Z'].sample(10) * "" #=> "eBoO8LnmIT"
require 'securerandom' SecureRandom.base64[0..9] #=> "Bm00Gza1eJ"