Use Iterators Instead of Nested Loops
In general, explicit looping in Ruby is not idiomatic, although there are always exceptions. You'll often find it easier to perform what you want using an object's native iterators.
Given a file like word_list.txt with a small set of contents like this:
a
b
c
d
e
f
g
you can use Array#combination to make your pairwise combinations. For example, with Ruby 3.0.0:
words = File.readlines('word_list.txt').map &:chomp
word_pairs = words.combination 2
File.open('new_list.txt', 'w') do |file|
word_pairs.map { file.puts "#{_1}#{_2}" }
end
You can then verify the results of your output in the shell:
$ cat new_list.txt
a b
a c
a d
a e
a f
a g
b c
b d
b e
b f
b g
c d
c e
c f
c g
d e
d f
d g
e f
e g
f g
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…