【发布时间】:2016-07-23 07:20:42
【问题描述】:
在 Windows 中我必须使用 cls 来清除控制台,在 Linux 中只需 clear!
有没有类似的函数:
system("cls") 或 system("clear")
【问题讨论】:
-
哦,好像没有很好的答案。除非您想安装
cursesgem,否则请使用 ANSI escape codes:print "\e[H\e[2J"。虽然它不那么便携。
在 Windows 中我必须使用 cls 来清除控制台,在 Linux 中只需 clear!
有没有类似的函数:
system("cls") 或 system("clear")
【问题讨论】:
curses gem,否则请使用 ANSI escape codes: print "\e[H\e[2J"。虽然它不那么便携。
不,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 模块的功劳归于 Aaron Hinni。
【讨论】: