I am looking for a method that has the same effect as Python itertools.productin Ruby. Take the following Python code:
from itertools import product
chars = []
for i in range(97,123):
chars.append(chr(i))
for a in range(1,3):
for i in product(chars,repeat=a):
s = ''.join(i)
print s
This outputs something like this:
a, b, c... x, y, z, aa, ab, ac... ax, ay, az, ba, bb, bc.. etc.
I tried translating this into Ruby:
(1..2).each do |n|
('a'..'z').to_a.combination(n).each do |c|
s = c.join
puts s
end
end
But the result is not the same. The single-character ones work fine (az), but when it goes into the two-character one, it doesn't work as I expected:
ab, ac, ad.. ax, ay, az, bc, bd, be
It does not generate aa, baor bb- does it mean that it generates all combinations without repeating characters or something else?
So, which method should be used to create all combinations, such as itertools.product, in Python?