Ruby 返回for循环并打印txt文件
我正在尝试一个代码,当for-loops方法生成字母时,它将打开一个文件,当这些字母连接在一起并形成'abcd'时,它将返回脚本并打印txt文件。 我尝试了不同的可能性,但目前只能停止脚本并打印所有时间字母,还打印了文件。但从来没能让所有人一起工作Ruby 返回for循环并打印txt文件,ruby,return,argv,Ruby,Return,Argv,我正在尝试一个代码,当for-loops方法生成字母时,它将打开一个文件,当这些字母连接在一起并形成'abcd'时,它将返回脚本并打印txt文件。 我尝试了不同的可能性,但目前只能停止脚本并打印所有时间字母,还打印了文件。但从来没能让所有人一起工作 def words(target) filename = ARGV.first txt = open(filename) ('a'..'z').each do |i| ('a'..'z').each do |j| (
def words(target)
filename = ARGV.first
txt = open(filename)
('a'..'z').each do |i|
('a'..'z').each do |j|
('a'..'z').each do |h|
('a'..'z').each do |g|
together = i+j+h+g
print together
case together
when together == target
return print txt.read
end
end
end
end
end
end
words("abcd")
提前感谢。这不是
案例的工作方式,如果发生以下情况,则应使用:
def words(target)
filename = ARGV.first
txt = open(filename)
('a'..'z').each do |i|
('a'..'z').each do |j|
('a'..'z').each do |h|
('a'..'z').each do |g|
together = i+j+h+g
print together
if together == target
return print txt.read
end
end
end
end
end
end
words("abcd")
或者,如果您坚持使用案例
(出于某种原因):
所需的输入/输出是什么?谢谢,我实际上也尝试了if,但仍然没有按预期工作。我不知道它在哪里失败了,因为我想我尝试过这个选项。不管怎么说,谢谢你,这对我很有用,即打印所有字母组合,从aaaa
到abcd
,然后打印文件内容。这不是你在问题中描述的吗?是的,这正是我想要的,我不知道它在哪里失败了,但是打印字母和返回并打印txt文件没有发生。现在一切都好了,我没有意识到重复排列。美好的
case together
when target
return print txt.read
end
('a'..'z').
to_a.
repeated_permutation(4).
map(&:join).
detect { |w| w == target }