【发布时间】:2016-07-14 15:41:26
【问题描述】:
我正在通过关注learnrubythehardway 学习 Ruby,在您被要求解释某些事情的作用之前,它似乎非常简单明了。我已经在我的代码中评论了我认为程序中正在发生的事情。想看看我是否达到目标,是否需要重新思考或不知道是否应该停止尝试学习如何编码。
# Sets variables to arguments sent through terminal
from_file, to_file = ARGV
# Saves from_file object into in_file
in_file = open(from_file)
puts in_file
# Saves from_file data into indata
indata = in_file.read
puts indata
# Save to_file object into out_file with writing privileages
out_file = open(to_file, 'w')
puts out_file
# Writes(copies) indata into out_file
out_file.write(indata)
# Closes files so they can not be accessed anymore
out_file.close
in_file.close
这是终端中的输出:
#<File:0x0000000201b038>
This is text from the ex17_from.txt file.
Did it copy to the ex17_to.txt file?
#<File:0x0000000201ae58>
我们还被赋予了尝试减少所需代码量的任务,并被告知我们可以在一行代码中完成整个操作。我想我可以删除所有的 cmets 和 puts 语句,而将其他所有内容放入一行代码中。但是,这将是一条长线,我认为这不是作者所要求的。有关如何缩短此代码的任何想法都会有所帮助。
【问题讨论】: