【问题标题】:How do I ask Ruby what system it's running on? [duplicate]我如何询问 Ruby 它在哪个系统上运行? [复制]
【发布时间】:2013-11-19 21:55:23
【问题描述】:

有没有办法询问 Ruby 它运行在什么操作系统上?

我将剪贴板 gem 与 Selenium 结合使用,我需要该脚本在所有系统上运行。

由于 OSX 和 Windows 上的“复制”键盘快捷键不同,我希望能够知道我的脚本在哪个操作系统上运行。

【问题讨论】:

标签: ruby


【解决方案1】:

可以使用RUBY_PLATFORM常量查询:

>> RUBY_PLATFORM
=> "i686-linux"

为此我们使用了类似于https://github.com/kotp/rlcw/blob/master/lib/platform.rb的代码:

module Platform

  def check_operating_system
    # The Mac check has to be proceed before the Win check!
    # Perhaps checking for /mswin/ will reduce this requirement.
    case RUBY_PLATFORM
    when /ix/i, /ux/i, /gnu/i, /sysv/i, /solaris/i, /sunos/i, /bsd/i
      require 'gtk2'
      Gtk.init
      @clip = Gtk::Clipboard.get(Gdk::Selection::CLIPBOARD)
      :unix
    when /darwin/i
      :mac_os_x
    when /mswin/i, /ming/i
      require 'vr/clipboard'
      @clip = Clipboard.new(2048)
      :windows
    else
      :other
    end
  end

end

【讨论】:

    猜你喜欢
    • 2010-09-15
    • 2020-04-22
    • 1970-01-01
    • 1970-01-01
    • 2015-01-12
    • 2022-11-23
    • 1970-01-01
    • 2013-01-02
    相关资源
    最近更新 更多