找出ruby正在运行的平台的最佳实践方法是什么?

时间:2021-08-18 20:57:54

I am using colored gem for coloured printing in the terminal and ruby logger. I need to run this code on linux and on windows.

我在终端和红宝石记录器中使用彩色宝石进行彩色打印。我需要在linux和windows上运行此代码。

On windows, I must first require 'win32console' or else coloured printing doesn't work (I just see ANSI escape characters instead). But if I require win32console on linux it breaks, obviously.

在Windows上,我必须首先要求'win32console',否则彩色打印不起作用(我只看到ANSI转义字符)。但是,如果我在linux上需要win32console,它显然会中断。

What's the usual way to handle a situation like this in ruby? I noticed the RUBY_PLATFORM variable, but on a windows VM I tried it was "i386-mingw32" or something strange. Using that plus a conditional seems like a pretty flakey way to go about what I need, so I was hoping this problem has a better solution.

在红宝石中处理这种情况的常用方法是什么?我注意到了RUBY_PLATFORM变量,但在Windows VM上,我试过它是“i386-mingw32”或者奇怪的东西。使用它加上条件似乎是一个非常好的方式来实现我需要的东西,所以我希望这个问题有一个更好的解决方案。

2 个解决方案

#1


1  

There's always:

总是:

begin
  require 'win32console'
rescue LoadError
end

I find this easier to write and reason about that trying to decide for myself which OS I'm on and whether or not to load it.

我发现这更容易编写和推理,试图自己决定我在哪个操作系统以及是否加载它。

Update: I was thinking win32console was built-in rather than a gem. I believe Win32API is available on all Windows installs, so it's a good proxy to test "Is this Windows?" (rather than "What OS is this, and is that Windows?").

更新:我认为win32console是内置的而不是gem。我相信Win32API适用于所有Windows安装,所以它是测试“这个Windows吗?”的好代理。 (而不是“这是什么操作系统,是Windows?”)。

begin
  require 'Win32API'
  windowsOS = true
rescue LoadError
  windowsOS = false
end

if windowsOS
  begin
    require 'win32console'
  rescue LoadError
    # Prompt user to install win32console gem
  end
end

#2


2  

Nothing wrong with using RUBY_PLATFORM, it is its purpose. You could also ask it the OS itself, for windows that would be

使用RUBY_PLATFORM没有错,这是它的目的。对于Windows来说,你也可以问操作系统本身

ENV['OS']

Which gives "Windows_NT" on a Vista.

这给了Vista上的“Windows_NT”。

Don't know the counterpart for the other OS.

不知道其他操作系统的对应物。

See also:

也可以看看:

#1


1  

There's always:

总是:

begin
  require 'win32console'
rescue LoadError
end

I find this easier to write and reason about that trying to decide for myself which OS I'm on and whether or not to load it.

我发现这更容易编写和推理,试图自己决定我在哪个操作系统以及是否加载它。

Update: I was thinking win32console was built-in rather than a gem. I believe Win32API is available on all Windows installs, so it's a good proxy to test "Is this Windows?" (rather than "What OS is this, and is that Windows?").

更新:我认为win32console是内置的而不是gem。我相信Win32API适用于所有Windows安装,所以它是测试“这个Windows吗?”的好代理。 (而不是“这是什么操作系统,是Windows?”)。

begin
  require 'Win32API'
  windowsOS = true
rescue LoadError
  windowsOS = false
end

if windowsOS
  begin
    require 'win32console'
  rescue LoadError
    # Prompt user to install win32console gem
  end
end

#2


2  

Nothing wrong with using RUBY_PLATFORM, it is its purpose. You could also ask it the OS itself, for windows that would be

使用RUBY_PLATFORM没有错,这是它的目的。对于Windows来说,你也可以问操作系统本身

ENV['OS']

Which gives "Windows_NT" on a Vista.

这给了Vista上的“Windows_NT”。

Don't know the counterpart for the other OS.

不知道其他操作系统的对应物。

See also:

也可以看看: