How can I clear the terminal in Ruby?

Ruby

Ruby Problem Overview


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.

Ruby Solutions


Solution 1 - Ruby

If you want something that is vaguely portable you can try:

system "clear" || system "cls"

which will try both clear and cls

Solution 2 - Ruby

Try any of these two in your ruby file:

puts `clear`

or

puts "\e[H\e[2J"

Solution 3 - Ruby

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

Solution 4 - Ruby

Here is a multiplatform way to do it:

Gem.win_platform? ? (system "cls") : (system "clear")

Solution 5 - Ruby

A slight variation works:

puts "Here's a very long string"
sleep 1
system ("cls")

Mark.

Solution 6 - Ruby

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

Solution 7 - Ruby

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]$ 

Solution 8 - Ruby

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.

Solution 9 - Ruby

clear_screen (Ruby 2.7+)

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.

Solution 10 - Ruby

If you are using MAC OS then use:

system('clear')

Solution 11 - Ruby

You can use system("clear") or system("cls") according to the terminal you are going to print.

  • If you are using the Windows Command Prompt, use system("cls").
  • If you are using a Mac or Linux system, just use 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

Solution 12 - Ruby

If you are on a Mac you can clear your terminal window with "Command + K".

Solution 13 - Ruby

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

Solution 14 - Ruby

Works on UNIX:

system("clear")

Solution 15 - Ruby

If you use Pry, It's very simple Just .clear

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMilanView Question on Stackoverflow
Solution 1 - RubyjbrView Answer on Stackoverflow
Solution 2 - RubySantiago Gomez LavinView Answer on Stackoverflow
Solution 3 - RubymatchewView Answer on Stackoverflow
Solution 4 - RubyarodView Answer on Stackoverflow
Solution 5 - RubyMark CView Answer on Stackoverflow
Solution 6 - RubyStephen RossView Answer on Stackoverflow
Solution 7 - RubySalilView Answer on Stackoverflow
Solution 8 - Rubyuser1475391View Answer on Stackoverflow
Solution 9 - RubyMarian13View Answer on Stackoverflow
Solution 10 - RubyFedorView Answer on Stackoverflow
Solution 11 - RubyMalith GammanpilaView Answer on Stackoverflow
Solution 12 - RubyJoshView Answer on Stackoverflow
Solution 13 - RubyBobBView Answer on Stackoverflow
Solution 14 - RubyDorianView Answer on Stackoverflow
Solution 15 - RubyLei CuiView Answer on Stackoverflow