I am trying to generate a array containing all two letter word combinations.
What would be the best way to generate it.
Could someone help me out?
As steenslag
points out, the quickest way is
('aa'..'zz').to_a
If your alphabet isn't 'a' through 'z', though, you can use Array#repeated_combination
:
alphabet = %w[А Б В Г Д Е Ё Ж З И Й К Л М Н О П Р С Т У Ф Х Ц Ч Ш Щ Ъ Ы Ь Э Ю Я]
alphabet.repeated_combination(2).map(&:join) # => ["AA", "AБ", ...]
Or, as Mladen
points out:
alphabet.product(alphabet).map(&:join)
Note: repeated_combination
is available in Ruby 1.9.2 or with require 'backports/1.9.2/array/repeated_combination'
from my backports
gem.
('aa'..'zz').to_a
Converts a Range to an Array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With