58

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.

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
Milan
  • 1,447
  • 5
  • 19
  • 27

15 Answers15

70

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

system "clear" || system "cls"

which will try both clear and cls

jbr
  • 6,198
  • 3
  • 30
  • 42
  • 4
    Please use `||` instead of `or` (See [here](https://github.com/rubocop-hq/ruby-style-guide#no-and-or-or)). The corresponding command would be `system("cls") || system("clear")` – Eric Duminil Jul 01 '19 at 06:39
  • 2
    For the _copy/pasters_ like me this may throw the error: `syntax error, unexpected string literal, expecting 'do' or '{' or '('` `system("clear") || system("cls")` fixes that. – Jay Killeen Jul 24 '20 at 04:29
29

Try any of these two in your ruby file:

puts `clear`

or

puts "\e[H\e[2J"
  • 9
    Please use the second one here. The idea of forking a process just to clear the screen makes my head hurt. See here for more information: http://www.termsys.demon.co.uk/vtansi.htm Also, check out ncurses for more robust control. But the chance that you'll find a non-ansi terminal these days is pretty much nil. – Michael Chaney Jul 03 '15 at 21:12
18

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
matchew
  • 19,195
  • 5
  • 44
  • 48
18

Here is a multiplatform way to do it:

Gem.win_platform? ? (system "cls") : (system "clear")
arod
  • 181
  • 1
  • 2
10

A slight variation works:

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

Mark.

Mark C
  • 615
  • 9
  • 15
6

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
HiDeoo
  • 10,353
  • 8
  • 47
  • 47
Stephen Ross
  • 376
  • 4
  • 17
6

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.

Marian13
  • 7,740
  • 2
  • 47
  • 51
4

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]$ 
Salil
  • 46,566
  • 21
  • 122
  • 156
3

If you are using MAC OS then use:

system('clear')
Fedor
  • 41
  • 2
3

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.

Wtower
  • 18,848
  • 11
  • 103
  • 80
3

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
2

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

Josh
  • 5,631
  • 1
  • 28
  • 54
1

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
BobB
  • 21
  • 2
  • 4
    This is dependent on how large someones screen buffer is set to and is far from efficient but anyways you can also do 50.times { puts " " } for short – Riptyde4 May 28 '15 at 22:30
1

Works on UNIX:

system("clear")
Dorian
  • 22,759
  • 8
  • 120
  • 116
-1

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

Lei Cui
  • 62
  • 1
  • 3