Skip to content

Instantly share code, notes, and snippets.

@joalbertg
Created August 24, 2020 20:22
Show Gist options
  • Save joalbertg/f5812a09d9fbdaaca5ff224c32db8128 to your computer and use it in GitHub Desktop.
Save joalbertg/f5812a09d9fbdaaca5ff224c32db8128 to your computer and use it in GitHub Desktop.
ruby: entrada y salida estándar

Ruby STDIN/STDOUT

cat in.txt | ruby stdin_stdout.rb > out.txt
ruby stdin_stdout.rb < in.txt > out.txt
3
1 2 3
11
10
5
0 9 8 7 6
[["1", "2", "3"]]
[["1", "2", "3"], "error"]
[["1", "2", "3"], "error", ["0", "9", "8", "7", "6"]]
# $stdin/$stdout son de la clase File
# STDIN/STDOUT
array = []
=begin
until (linea = $stdin.gets).nil?
next if linea.is_a? Numeric
linea = linea.to_i
str = $stdin.gets.split(' ')
if str.size == linea
array.push(str)
else
array.push('error')
end
$stdout.puts array.to_s
end
=end
until (linea = STDIN.gets).nil?
next if linea.is_a? Numeric
linea = linea.to_i
str = STDIN.gets.split(' ')
if str.size == linea
array.push(str)
else
array.push('error')
end
STDOUT.puts array.to_s
end
# input
=begin
3
1 2 3
11
10
5
0 9 8 7 6
=end
# output
=begin
[["1", "2", "3"]]
[["1", "2", "3"], "error"]
[["1", "2", "3"], "error", ["0", "9", "8", "7", "6"]]
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment