在 Interactive Ruby (irb) 中清空屏幕How to Clear the Console in Interactive Ruby (irb)

因为强迫症雷达喵的原因,于是有了这篇文章。

1
->(a,b,c){x=a.method(b);a.send(c,b){send c,b,&x;false};print"\e[2J\e[H \e[D"}[irb_context,:echo?,:define_singleton_method]
Windows / Linux, 无回显无空行w/o echo and empty lines

这玩意儿可以将 IRB 屏幕清理地干干净净,既没有空行也没有 => nil。在 Linux/Windows 上都测试通过。
This will fully clear your IRB screen, with no extra empty lines and “=> nil” stuff. Tested on Linux/Windows.


这一行可以展开成这样:
This one-liner could be expanded as:

1 2 3 4 5 6 7 8
lambda {
original_echo = irb_context.method(:echo?)
irb_context.send(:define_singleton_method, :echo?) {
send :define_singleton_method, :echo?, &original_echo
false
}
print "\e[2J\e[H \e[D"
}.call
Windows / Linux, 无回显无空行(展开)w/o echo and empty lines (expanded)

这儿用了不少黑科技呢……

首先,irb 会调用 echo? 来判断是否应该回显。我这里先把他存起来,用一个调用之后会恢复他的函数覆盖。当然,覆盖用的函数会返回 false 所以 irb 就不会回显结果了。

然后,打印了几个 ANSI 控制字符。\e[2J 用来清屏,\e[H 把光标移动到左上角。 \e[D 打印一个空格再马上退回光标,这是用来解决 Windows 上奇怪问题的。

总而言之,谁没事会打这么多来清屏啊……不过,把他存进你的 .irbrc 里倒是不错。

This uses lots of tricks.

Firstly, irb will call echo? to check if the result should be printed. I saved the method, then redefined with a method which restores the definition but returns false so irb will not echo the result.

Secondly, I printed some ANSI control chars. \e[2J will clean the screen and \e[H will move the cursor to the upper left position of the screen.  \e[D will print a space and then move back the cursor while this is a workaround for something strange on Windows.

Finally this is kind of not practical at all except you are saving this code in your .irbrc.


为了干掉空行,确实挺麻烦。但要是只想干掉回显倒是很简单。
Whilst it needs really a lot to skip the empty line, it's easy to omit the echo only.

1
->(x){def x.inspect;self;end;x}["\e[H\e[2J"]
Windows / Linux 无回显有空行w/o echo but w/ empty lines

这一行可以展开成这样:
This one-liner could be expanded as:

1 2 3 4 5 6 7
lambda {
x = "\e[H\e[2J"
def x.inspect
self
end
x
}.call
Windows / Linux 无回显有空行(展开)w/o echo but w/ empty lines (expanded)

好了雷达喵你玩儿蛋去。


By the way, you can read this ruby – How Do You Clear The IRB Console? on Stack Overflow.
然后以下是对 ruby – How Do You Clear The IRB Console? – Stack Overflow 的部分粗糙翻译。

楼主 John Topley:你们怎么在 IRB 里清屏?

106 票 John Topley:在 Mac OS XLinux 上你可以用 Ctrl + L 来清空屏幕。

29 票 Ben HoffsteinWindows 里,把这个代码丢在 %userprofile%\.irbrc 里:

1 2 3
def cls
system('cls')
end

11 票 TW Scannell:在 Ubuntu 11.10clear 差不多就行了,只是你会看到 => true,把一切搞得很乱。

1
ruby-1.9.2-p290 :007 > system 'clear'
1 2
=> true
ruby-1.9.2-p290 :007 >

8 票 AShelly:在 *nix`clear` 就行了。有趣的是,在 Windows 上用 system 'cls' 就行,`clear` 却不行。

Version History版本历史

  • 36a4eb7 Add translations & details into How to Clear the Console in IRB
  • 0143959 I18N changes: Add language field to posts
  • 0f1cf5d Add Fist code box
  • 4a8e516 Rename categories: stuff -> data, coding -> tech
  • 48d338c Remove yourls_shorturl, categories and tags & add keybase.txt
  • e1cb087 Fix some formatting & broken links; remove yourls_shorturl, categories and tags
  • 5b93ba5 Remove author & delete some broken posts
  • 1ee024d Import posts from my WordPress blog