开发者

How can I clear the terminal in Ruby?

开发者 https://www.devze.com 2023-01-06 09:06 出处:网络
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

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 i开发者_JAVA百科t 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


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.


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


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.


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


If you are using MAC OS then use:

system('clear')


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.

  • 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


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


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


Works on UNIX:

system("clear")


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

0

精彩评论

暂无评论...
验证码 换一张
取 消