使用 crystal-lang 无需按回车即可从 $stdin 读取整行
Read whole lines from $stdin without pressing enter with crystal-lang
这是一个类似的问题
如何使用 crystal-lang 阅读整行?我假设使用以下 Ruby 等效代码:
lines = $stdin.read
lines.each{|line| puts line}
同样,您使用 STDIN.raw
,但这次您希望使用 IO#gets
一次获取整行。最简单的方法是:
while line = STDIN.raw &.gets
puts line
end
或者您可以这样做:
STDIN.raw do |stdin|
stdin.each_line do |line|
puts line
end
end
通过在线编译器使用此代码
我直接用了STDIN
STDIN.each_line do |line|
puts line
end
显然 read
相当于 STDIN.gets_to_end
FWIW。
https://groups.google.com/forum/#!topic/crystal-lang/O4DExFHJc5E
这是一个类似的问题
如何使用 crystal-lang 阅读整行?我假设使用以下 Ruby 等效代码:
lines = $stdin.read
lines.each{|line| puts line}
同样,您使用 STDIN.raw
,但这次您希望使用 IO#gets
一次获取整行。最简单的方法是:
while line = STDIN.raw &.gets
puts line
end
或者您可以这样做:
STDIN.raw do |stdin|
stdin.each_line do |line|
puts line
end
end
通过在线编译器使用此代码
我直接用了STDIN
STDIN.each_line do |line|
puts line
end
显然 read
相当于 STDIN.gets_to_end
FWIW。
https://groups.google.com/forum/#!topic/crystal-lang/O4DExFHJc5E