如果没有参数,则针对标准输入运行的脚本;否则输入文件=ARGV[0]
这工作得很好 - 只是想知道是否有任何改进可以缩短它?
if (ARGV[0].nil?) then
input=$<
else
input=File.new(ARGV[0],"r");
end
...
# Do something with the input here, for example:
input.each_line do |line|
puts line
end
This works quite nicely - just wondered if there are any improvements to shorten it ?
if (ARGV[0].nil?) then
input=lt;
else
input=File.new(ARGV[0],"r");
end
...
# Do something with the input here, for example:
input.each_line do |line|
puts line
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以完全删除前五行。
来自镐
因此:
Kernel.gets 是 $<.gets 的简写,因此:
You can eliminate the first five lines entirely.
From Pickaxe
Therefore:
Kernel.gets is shorthand for $<.gets, so:
只有
ARGV ?
对我有用,"r"
通常是默认的,所以可以跳过它,File.new()
可能与相同>文件()
,所以Only
ARGV ?
works for me,"r"
normally default so can skip it, andFile.new()
may be same toFile()
, Sothen
和;
是可选的,您也可以使用三元运算符:
then
and;
are optionalalso you can use the ternary operator: