Ruby - creating all two-letter words

I am trying to create an array containing all two-word combinations of words.

What will be the best way to create it.

Can anyone help me out?

+5
source share
2 answers

As pointed out steenslag, the fastest way is

('aa'..'zz').to_a

If your alphabet is not "a" through "z", you can use Array#repeated_combination:

alphabet = %w[                                ]
alphabet.repeated_combination(2).map(&:join) # => ["AA", "A", ...]

Or, as indicated by Mladen:

alphabet.product(alphabet).map(&:join)

Note: repeated_combinationAvailable in Ruby 1.9.2 or require 'backports/1.9.2/array/repeated_combination'from my backportsgem.

+20
source
('aa'..'zz').to_a

Converts a range to an array.

+8
source

All Articles