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")
I want the console to be cleared after executing this commnad, but it is not working.
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")
I want the console to be cleared after executing this commnad, but it is not working.
If you want something that is vaguely portable you can try:
system "clear" || system "cls"
which will try both clear
and cls
||
instead of or
(See here). The corresponding command would be system("cls") || system("clear")
Commented
Jul 1, 2019 at 6:39
syntax error, unexpected string literal, expecting 'do' or '{' or '('
system("clear") || system("cls")
fixes that.
Commented
Jul 24, 2020 at 4:29
Try any of these two in your ruby file:
puts `clear`
or
puts "\e[H\e[2J"
Edit: (rereading your question I realize this is not what you are after. I thought you were referring to the IRB. I will leave this here and not delete it as I feel it is can be very useful information)
Ultimately it depends what system you are on.
ctrl+l (<- that is a lower case L) will clear the terminal ( cmd+K on a mac I believe)
this also works in the regular terminal, or the python interprator, or mysql, etc
there are a fair amount of other shortcuts you may like to familiarize yourself with. i found this after a quick google search:
CTRL-l - Clears the screen and places the command prompt at the top of the page.
CTRL-r - Starts a search against the command history. Start by typing in what you want to search by then press CTRL-r to see the matches.
CTRL-c - Kills the current running foreground program.
CTRL-z - Stop/sleep the current running foreground program.
CTRL-s - Stops the output to the screen.
CTRL-q - Allows output to the screen.
CTRL-a - Moves the cursor the start of the line
CTRL-e - Moves the cursor to the end of the line
CTRL-f - Moves the cursor 1 character forward
CTRL-b - Moves the cursor 1 character backward
not mentioned on that list is that
Alt-F moves the curosor one word forward
Alt- B moves the cursor one word back
Here is a multiplatform way to do it:
Gem.win_platform? ? (system "cls") : (system "clear")
A slight variation works:
puts "Here's a very long string"
sleep 1
system ("cls")
Mark.
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.
This should cover windows and OSX/Linux terminals.
def method_name
puts "amit"
if RUBY_PLATFORM =~ /win32|win64|\.NET|windows|cygwin|mingw32/i
system('cls')
else
system('clear')
end
end
method_name
You can use following create a ruby file say check.rb like follwing
puts "amit"
#system "clear"
and run it from console [Salil@localhost Desktop]$ check.rb
o/p
[Salil@localhost Desktop]$ ruby check.rb
amit
[Salil@localhost Desktop]$
now modify check.rb and run it from console
puts "amit"
system "clear"
o/p
[Salil@localhost Desktop]$
For windows users:
Just type this below function in your irb window and you are good to go:
Define this function:
def cls
system('cls')
end
After defining call this function whenever required.
You can use system("clear")
or system("cls")
according to the terminal you are going to print.
system("cls")
.system("clear")
.Or you can use a better way. Check this example.
count = 0
until count == 10
system("cls") || system("clear")
print count
count += 1
sleep 1
end
A portable, compromized yet often visually satisfying approach that I use is what I call "crazy putz puts":
counter=0
until counter == 50
puts " "
counter += 1
end