10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/strandom.rb', line 10
def rand(len: 0, type: :alnum, cust: [])
num = (48..57).to_a
upper = (65..90).to_a
lower = (97..122).to_a
case type
when :alpha
range = upper + lower
when :upper
range = upper
when :upnum
range = num + upper
when :lower
range = lower
when :lownum
range = num + lower
when :num
range = num
when :custom
range = cust
else
range = num + upper + lower
end
if type == :cust
([nil] * len).map { range.sample }.join
else
([nil] * len).map { range.sample.chr }.join
end
end
|