我想知道如何在Ruby中实现类似于C语言中 system("clear")
的操作。
我写了一段程序,类似于
puts "amit"
system("clear")
我想在执行该命令后清除控制台,但它并没有起作用。
我想知道如何在Ruby中实现类似于C语言中 system("clear")
的操作。
我写了一段程序,类似于
puts "amit"
system("clear")
我想在执行该命令后清除控制台,但它并没有起作用。
如果你想要相对便携的东西,你可以尝试:
system "clear" || system "cls"
它将尝试清除clear
和cls
命令。
||
д»Јжӣҝor
пјҲеҸӮи§ҒжӯӨеӨ„пјүгҖӮзӣёеә”зҡ„е‘Ҫд»Өеә”иҜҘжҳҜsystem("cls") || system("clear")
гҖӮ - Eric Duminilsyntax error, unexpected string literal, expecting 'do' or '{' or '('
system("clear") || system("cls")
可以解决这个问题。 - Jay Killeen在你的 Ruby 文件中尝试以下两种方法:
puts `clear`
或者puts "\e[H\e[2J"
编辑:(重新阅读您的问题后,我意识到这不是您所想要的。我以为您在提到IRB。 我会将其保留在此处,不删除,因为我觉得它可能是非常有用的信息)
最终取决于您使用的系统。
ctrl+l (<- 这是小写 L)将清除终端窗口(在Mac上是 cmd+K)。
在常规终端、Python 解释器或 MySQL 中等也同样适用。
还有很多其他快捷键可以让您熟悉和使用。我在快速搜索后找到了这个:
CTRL-l - Clears the screen and places the command prompt at the top of the page.
CTRL-r - Starts a search against the command history. Start by typing in what you want to search by then press CTRL-r to see the matches.
CTRL-c - Kills the current running foreground program.
CTRL-z - Stop/sleep the current running foreground program.
CTRL-s - Stops the output to the screen.
CTRL-q - Allows output to the screen.
CTRL-a - Moves the cursor the start of the line
CTRL-e - Moves the cursor to the end of the line
CTRL-f - Moves the cursor 1 character forward
CTRL-b - Moves the cursor 1 character backward
那个列表上没有提到的是
Alt-F moves the curosor one word forward
Alt- B moves the cursor one word back
这是一种跨平台的做法:
Gem.win_platform? ? (system "cls") : (system "clear")
puts "Here's a very long string"
sleep 1
system ("cls")
马克。
def method_name
puts "amit"
if RUBY_PLATFORM =~ /win32|win64|\.NET|windows|cygwin|mingw32/i
system('cls')
else
system('clear')
end
end
method_name
对于Windows用户:
只需在irb窗口中输入以下函数即可:
定义此函数:
def cls
system('cls')
end
在定义后,需要时调用此函数。
您可以使用以下内容创建一个名为check.rb的Ruby文件:
puts "amit"
#system "clear"
并从控制台运行它 [Salil@localhost Desktop]$ check.rb
输出
[Salil@localhost Desktop]$ ruby check.rb
amit
[Salil@localhost Desktop]$
现在修改 check.rb 并从控制台运行它
puts "amit"
system "clear"
o/p
[Salil@localhost Desktop]$
system('clear')