1

It's a similar question that Read a single char from stdin without pressing enter

How do I read whole lines with crystal-lang ? I assume to use following Ruby equivalent code:

lines = $stdin.read
lines.each{|line| puts line}
hiropon
  • 1,675
  • 2
  • 18
  • 41

3 Answers3

2

Again, you use STDIN.raw but this time you want to fetch a whole line at a time using IO#gets. The easiest way to do that is this:

while line = STDIN.raw &.gets
  puts line
end

Alternatively you could do this:

STDIN.raw do |stdin|
  stdin.each_line do |line|
    puts line
  end
end
Stephie
  • 3,135
  • 17
  • 22
  • thanks but, it seems #raw method always requires tty. it's inconvenient for me because I wanted using it for competitive programming – hiropon Jul 20 '17 at 16:46
1

To use this code with online compiler

I just used STDIN directly

STDIN.each_line do |line|
  puts line
end
hiropon
  • 1,675
  • 2
  • 18
  • 41
0

Apparently the equivalent of read is STDIN.gets_to_end FWIW.

https://groups.google.com/forum/#!topic/crystal-lang/O4DExFHJc5E

rogerdpack
  • 62,887
  • 36
  • 269
  • 388