【问题标题】:How can i use Commands from more then one operating- system in Ruby?如何在 Ruby 中使用来自多个操作系统的命令?
【发布时间】:2016-07-23 07:20:42
【问题描述】:

在 Windows 中我必须使用 cls 来清除控制台,在 Linux 中只需 clear

有没有类似的函数:

system("cls")system("clear")

【问题讨论】:

标签: ruby linux windows macos


【解决方案1】:

不,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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-25
    • 2018-03-28
    • 2013-02-01
    • 1970-01-01
    • 2014-07-31
    • 2014-01-23
    • 2019-05-13
    • 2020-07-21
    相关资源
    最近更新 更多