0
在Windows中,我必须使用cls
来清除控制台,在Linux中只需要clear
!如何在Ruby中使用多个操作系统的命令?
是否有一个功能,如:
system("cls")
或system("clear")
在Windows中,我必须使用cls
来清除控制台,在Linux中只需要clear
!如何在Ruby中使用多个操作系统的命令?
是否有一个功能,如:
system("cls")
或system("clear")
没有,一个已经写好方法对于这两个不存在于Ruby的本地库。然而,你可以做这样的事情:
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
则:
def clear_console
if OS.windows?
system("cls")
else
system("clear")
end
end
归功于Aaron Hinni为OS
模块。
可能重复的[如何清除终端在Ruby?](https://stackoverflow.com/questions/3170553/how-can-i-clear-the-terminal-in-ruby/) – cremno
哦,它似乎没有一个好的答案。除非你想安装'curses' gem,否则使用[ANSI转义码](https://en.wikipedia.org/wiki/ANSI_escape_code):'print'\ e [H \ e [2J“'。虽然它不是那么便携。 – cremno