0

In Windows I have to use cls to clear the Console, in Linux just clear!

Is there a Function like:

system("cls") or system("clear")

thor
  • 21,418
  • 31
  • 87
  • 173
  • possible duplicate of [How can I clear the terminal in Ruby?](https://stackoverflow.com/questions/3170553/how-can-i-clear-the-terminal-in-ruby/) – cremno Apr 03 '16 at 21:27
  • 1
    Oh, it doesn't seem to have a good answer. Unless you want to install the `curses` gem, use [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code): `print "\e[H\e[2J"`. It's not as portable though. – cremno Apr 03 '16 at 21:30

1 Answers1

0

Nope, an already written method for both does not exists in ruby native libs. However, you can do something like that :

module OS
  def OS.windows?
    (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
  end

  def OS.mac?
   (/darwin/ =~ RUBY_PLATFORM) != nil
  end

  def OS.unix?
    !OS.windows?
  end

  def OS.linux?
    OS.unix? and not OS.mac?
  end
end

then :

def clear_console
    if OS.windows?
      system("cls")
    else
      system("clear")
    end
end

credit goes to Aaron Hinni for the OS module.

Community
  • 1
  • 1
dvxam
  • 604
  • 4
  • 12