我想for
根据变量中的值在自己中放置尽可能多的循环。
例如,如果变量@var = 1
,我需要执行:
for letter1 in @range
do something
end
如果变量@var = 2
:
for letter1 in @range
for letter2 in @range
do something
end
end
如果变量@var = 3
for letter1 in @range
for letter2 in @range
for letter3 in @range
do something
end
end
end
有没有比下面的代码更智能/更少的代码方式?我不想重蹈覆辙。
if @var == 1
for letter2 in @range
do something
end
elsif @var == 2
for letter1 in @range
for letter2 in @range
do something
end
end
elsif @var == 3
for letter1 in @range
for letter2 in @range
for letter3 in @range
do something
end
end
end
end
这do something
部分在每种情况下总是相同的。不同之处仅在于它在多少个周期内。