I would like to know to how to do in Ruby what I can do with system(\"clear\")
in C.
I wrote a program like
puts \"amit\"
system(\"clear\")
Works on UNIX:
system("clear")
If you are on a Mac you can clear your terminal window with "Command + K".
If you want something that is vaguely portable you can try:
system "clear" || system "cls"
which will try both clear
and cls
A slight variation works:
puts "Here's a very long string"
sleep 1
system ("cls")
Mark.
Here is a multiplatform way to do it:
Gem.win_platform? ? (system "cls") : (system "clear")
Starting from Ruby 2.7, there is a build-in and cross-platform way to clear the terminal output:
require 'io/console'
$stdout.clear_screen # or STDOUT.clear_screen
See the difference between $stdout
and STDOUT
here.