DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Determine The Operating System In Ruby

10.09.2008
| 15834 views |
  • submit to reddit
        This code runs on RUBY_PLATFORM which returns the platform the active copy of ruby was
compiled to run on.  I have not found a better method thus far.

module OsFunctions
  
  # universal-darwin9.0 shows up for RUBY_PLATFORM on os X leopard with the bundled ruby. 
  # Installing ruby in different manners may give a different result, so beware.
  # Examine the ruby platform yourself. If you see other values please comment
  # in the snippet on dzone and I will add them.

  def is_mac?
    RUBY_PLATFORM.downcase.include?("darwin")
  end

  def is_windows?
     RUBY_PLATFORM.downcase.include?("mswin")
  end

  def is_linux?
     RUBY_PLATFORM.downcase.include?("linux")
  end
end
    

Comments

Daniel Berger replied on Thu, 2007/07/12 - 5:21pm

Bad idea. RUBY_PLATFORM will return 'java' when using JRuby for example. See the sys-uname library for much more detailed information. gem install sys-uname