我正在使用 ruby 开发一个简单的编码解码程序。编码器工作正常,但是,我无法正确解码文件。它总是返回一个空白文本文件,而不是一个很好解码的文件。这是代码:
编码器,它工作得很好:
# Get evil idea and encode
print "Enter your diabolical scheme: "
$idea = gets
$encode1 = $idea.reverse
$encode2 = $encode1.capitalize
print $encode2
print"
"
#Save the jibberish to a new file
print "
File encoded. Please enter a name for this idea: "
idea_name = gets.strip
File::open( "idea-" + idea_name + ".txt", "w" ) do |f|
f << $encode2
end
还有解码器,完全搞砸了:
#Get file to decode
print 'Name of file to decode:'
$name = gets
$file = $name << '.txt'
Dir[$file].each do |file_name|
$idea = read.File($file)
end
# Print each idea out with the words fixed
$decode1 = $idea.to_s()
$decode2 = $decode1.reverse()
$decode3 = $decode2.capitalize()
print $decode3
#Save decoded file to new location
print "
File decoded. Please enter a new name for this idea: "
idea_name = gets.strip
File::open( idea_name + ".txt", "w" ) do |f|
f << $decode3
end
如何让解码器正确解码文本?