Combining two different 'ranges' to one in ruby

Radhika picture Radhika · Jan 28, 2014 · Viewed 7.2k times · Source

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:

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

Which obviously did not work. I know I might be overlooking some easy way to do this but I am unable to find one, any help would be much appreciated. Thanks in advance:)

Answer

kmikael picture kmikael · May 25, 2014

I have a nicer method: use the splat operator!

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