LoginSignup
15
12

More than 5 years have passed since last update.

RubyでOSを判別する

Last updated at Posted at 2016-05-07

OS依存なコードを書く機会があり、OSを判別する必要があったのでメモ

require 'rbconfig'

def os
  @os ||= (
    host_os = RbConfig::CONFIG['host_os']
    case host_os
    when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
      :windows
    when /darwin|mac os/
      :macosx
    when /linux/
      :linux
    when /solaris|bsd/
      :unix
    else
      :unknown
    end
  )
end

こちらのコードほぼそのままです。。。

RUBY_PLATFORMを使う例もいくつか見かけたのですが、スクリプトをJRubyで動かした時にRUBY_PLATFORM'java'を返すのは具合が悪かったので、今回は上記の方法を採用しました。

15
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
12