ruby-on-railsarraysshufflerange

Combining two different 'ranges' to one in ruby


I want to combine two different ranges in rails into a single array. Is there any short method for the same?

I am writing code to generate random alpha-numeric strings.

Right now I have:

('a'..'z').to_a.shuffle.first(16).join

I have also tried something like this (which did not work):

('a'..'z').to_a.push('0'..'9').shuffle.first(16).join

Solution

  • I have a nicer method: use the splat operator!

    [*'0'..'9', *'a'..'z', *'A'..'Z'].sample(16).join