【问题标题】:How to use bash clear from within a ruby cli如何在 ruby​​ cli 中使用 bash clear
【发布时间】:2013-12-14 15:26:43
【问题描述】:

当我只是在终端中浏览文件时,Clear 可以正常工作。但是在我的 CLI 程序中,

我已尝试清除,但无法清除用户的屏幕。

`clear`

为什么这目前在我的 CLI 代码中不起作用?

睡眠有效。

`sleep 5`


  def dealer_hits_until_17
    @new_hand = false
    while hand_score(@dealer_hand) < 17 && @new_hand == false
      puts "Dealer Hits! His hand is now #{@dealer_hand}xxx"
      hit(@dealer_hand)
      `sleep 1`
      `clear`
    end
    if @new_hand == false
      compare_hand_value
    end
  end

有谁知道我可以在我的 Ruby CLI 中添加什么来清除屏幕?

【问题讨论】:

  • 只要仔细研究文档,如果你没有找到你问的问题Ruby#Kernel

标签: ruby bash terminal command-line-interface


【解决方案1】:

clear 输出 ANSI 序列以清除屏幕。 `cmd` 捕获cmd 命令的输出并返回它。这会阻止屏幕清晰。

Kernel#system代替`..`来清屏:

system('clear')

【讨论】:

  • @bliof,使用puts,您会打印出额外的换行符。恕我直言,应该改用print `clear`。感谢您的评论。
猜你喜欢
  • 2021-04-22
  • 2015-05-13
  • 2018-11-28
  • 2011-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多