How can I clear the terminal in Ruby?

前端 未结 15 1636
心在旅途
心在旅途 2020-11-30 06:14

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\")


        
相关标签:
15条回答
  • 2020-11-30 06:46

    Works on UNIX:

    system("clear")
    
    0 讨论(0)
  • 2020-11-30 06:48

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

    0 讨论(0)
  • 2020-11-30 06:51

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

    system "clear" || system "cls"
    

    which will try both clear and cls

    0 讨论(0)
  • 2020-11-30 06:54

    A slight variation works:

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

    Mark.

    0 讨论(0)
  • 2020-11-30 06:55

    Here is a multiplatform way to do it:

    Gem.win_platform? ? (system "cls") : (system "clear")
    
    0 讨论(0)
  • 2020-11-30 06:56

    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.

    0 讨论(0)
提交回复
热议问题