As I'm learning Ruby I wrote this little program to generate a "nice" number randomly (like 4747, 6969, etc.). As I'm currently breaking the "do not repeat yourself" rule, how would you refactor the following?
def nice_numbers
a = rand(9)
b = rand(9)
c = rand(9)
d = rand(9)
while a != c || b != d
a = rand(9)
b = rand(9)
c = rand(9)
d = rand(9)
end
nice = [a,b,c,d].join
end
puts nice_numbers
c
andd
at all - what is the purpose if you always wanta == c
andb == d
? It would save you almost all your code if you just did[a,b,a,b].join
at the end rather than force a "random" number to match. In fact it's a one liner([rand(9),rand(9)]*2).join
\$\endgroup\$