hiropon
hiropon

Reputation: 1802

Read whole lines from $stdin without pressing enter with crystal-lang

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}

Upvotes: 2

Views: 1069

Answers (3)

rogerdpack
rogerdpack

Reputation: 66771

Apparently the equivalent of read is STDIN.gets_to_end FWIW.

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

Upvotes: 0

hiropon
hiropon

Reputation: 1802

To use this code with online compiler

I just used STDIN directly

STDIN.each_line do |line|
  puts line
end

Upvotes: 1

Stephie
Stephie

Reputation: 3175

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

Upvotes: 2

Related Questions