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.

2
  • 1
    What operating system are you running Ruby on?
    – Zabba
    Commented Dec 30, 2010 at 4:01
  • SCiTE is not a compiler, it's just a text editor. Commented Apr 4, 2012 at 14:48

15 Answers 15

70

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

system "clear" || system "cls"

which will try both clear and cls

2
  • 5
    Please use || instead of or (See here). The corresponding command would be system("cls") || system("clear") Commented Jul 1, 2019 at 6:39
  • 3
    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. Commented Jul 24, 2020 at 4:29
31

Try any of these two in your ruby file:

puts `clear`

or

puts "\e[H\e[2J"
1
  • 10
    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: 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. Commented Jul 3, 2015 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
17

Here is a multiplatform way to do it:

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

A slight variation works:

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

Mark.

0
7

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.

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

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.

1
  • This is a great answer for most operating systems, but it sadly doesn't work on things like UNIX :(
    – user17585064
    Commented May 17, 2022 at 23:49
4

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
3

If you are using MAC OS then use:

system('clear')
2

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

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
1
  • 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
    Commented May 28, 2015 at 22:30
1

Works on UNIX:

system("clear")
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.