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?
As pointed out steenslag, the fastest way is
steenslag
('aa'..'zz').to_a
If your alphabet is not "a" through "z", you can use Array#repeated_combination:
Array#repeated_combination
alphabet = %w[ ] alphabet.repeated_combination(2).map(&:join) # => ["AA", "A", ...]
Or, as indicated by Mladen:
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.
repeated_combination
require 'backports/1.9.2/array/repeated_combination'
backports
Converts a range to an array.