How can I clear the terminal in Ruby?

前端 未结 15 1637
心在旅途
心在旅途 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:57

    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
    
    0 讨论(0)
  • 2020-11-30 06:58

    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 讨论(0)
  • 2020-11-30 07:00

    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.

    0 讨论(0)
  • 2020-11-30 07:04

    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
    
    0 讨论(0)
  • If you are using MAC OS then use:

    system('clear')
    
    0 讨论(0)
  • 2020-11-30 07:09

    Try any of these two in your ruby file:

    puts `clear`
    

    or

    puts "\e[H\e[2J"
    
    0 讨论(0)
提交回复
热议问题