如何在Ruby中使用stdin和stdout
因此,我有一个下面的方法,它可以工作,而不使用stdin或stdout如何在Ruby中使用stdin和stdout,ruby,stdout,stdin,Ruby,Stdout,Stdin,因此,我有一个下面的方法,它可以工作,而不使用stdin或stdout def main(lines) lines.each_index do |i| word = lines[i] if word.length > 1 && word.length <=11 puts "I use #{word}" end end end main(["Google", "yahoo", "stackoverflow", "reddit"])
def main(lines)
lines.each_index do |i|
word = lines[i]
if word.length > 1 && word.length <=11
puts "I use #{word}"
end
end
end
main(["Google", "yahoo", "stackoverflow", "reddit"])
我不知道如何实现这样的命令行
对于stdout,它会出现在put
之前吗
stdout.puts "I use #{word}"
使用以下方法之一:
$stdout.puts "I use #{word}"
或
有关详细信息,请参阅“”
您可以简化脚本:
def main(lines)
lines.each_with_index do |i,v| # or use each instead if u just want only the value and change |i,word| to |word|
if word.length > 1 && word.length <=11
puts "I use #{word}"
end
end
end
def干管(管路)
如果只需要值并将| i,word |更改为| word,则使用| i,v |#或使用每个|
如果word.length>1&&word.length,则它们同时定义为全局变量和常量。您可以使用$stdout.put
或stdout.put
。同样适用于$stderr
/stderr
。有关详细信息,请参阅文档。您调用的put
方法是–文档提到它相当于$stdout.put(…)
考虑编写line.each do | line |;如果(1..11).cover?(line.length),则放入“我使用#{line}”;结束
。Ruby默认使用STDIN/STDOUT读取和写入普通控制台,因此不要显式使用通道指示符,除非它指向非标准通道,如文件。没有正当理由添加它们只会给维护代码的其他人带来视觉上的噪音和潜在的混乱。“始终编写代码,就好像最终维护你的代码的人是一个知道你住在哪里的暴力精神病患者。代码的可读性。”-约翰·伍兹目前还不清楚你为什么要这样做。见“”。一个用例可能会澄清这一点。
STDOUT.puts "I use #{word}"
def main(lines)
lines.each_with_index do |i,v| # or use each instead if u just want only the value and change |i,word| to |word|
if word.length > 1 && word.length <=11
puts "I use #{word}"
end
end
end