Sha256: 2b03e1b13daf4fd7584a7903abef2274c5c3f18a242b6f9a5486a9e861a6e714

Contents?: true

Size: 1.78 KB

Versions: 25

Compression:

Stored size: 1.78 KB

Contents

module Sys

  #
  # Return the current operating system: Darwin, Linux, or Windows.
  #
  def self.os
    return @os if @os

    require 'rbconfig'
    if defined? RbConfig
      host_os = RbConfig::CONFIG['host_os']
    else
      host_os = Config::CONFIG['host_os']
    end

    case host_os
      when /darwin/
        @os = "Darwin"
      when /bsd/
        @os = "BSD"
      when /linux/
        @os = "Linux"
      when /mingw|mswin|cygwin/
        @os = 'Windows'
    else
      #raise "Unknown OS: #{host_os.inspect}"
    end

    @os
  end

  #
  # Is this Linux?
  #
  def self.linux?
    os == "Linux"
  end

  #
  # Is this Windows?
  #
  def self.windows?
    os == "Windows"
  end

  #
  # Is this Darwin?
  #
  def self.darwin?
    os == "Darwin"
  end

  #
  # Is this a Mac? (aka. Darwin?)
  #
  def self.mac?; darwin?; end

  #
  # Is this BSD?
  #
  def self.bsd?
    os == "BSD" or os == "Darwin"
  end


  #
  # A metaprogramming helper that allows you to write platform-specific methods
  # which the user can call with one name. Here's how to use it:
  #
  # Define these methods:
  #   reboot_linux, reboot_darwin, reboot_windows
  #
  # Call the magic method:
  #   cross_platform_method(:reboot)
  #
  # Now the user can execute "reboot" on any platform!
  #
  # (Note: If you didn't create a method for a specific platform, then you'll get
  # NoMethodError exception when the "reboot" method is called on that platform.)
  #
  def self.cross_platform_method(name)
    platform_method_name = "#{name}_#{os.downcase}"
    metaclass.instance_eval do
      define_method(name) do |*args|
        begin
          self.send(platform_method_name, *args)
        rescue NoMethodError
          raise NotImplementedError.new("#{name} is not yet supported on #{os}.")
        end
      end
    end
  end

end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
epitools-0.5.136 lib/epitools/sys/os.rb
epitools-0.5.134 lib/epitools/sys/os.rb
epitools-0.5.133 lib/epitools/sys/os.rb
epitools-0.5.131 lib/epitools/sys/os.rb
epitools-0.5.130 lib/epitools/sys/os.rb
epitools-0.5.129 lib/epitools/sys/os.rb
epitools-0.5.128 lib/epitools/sys/os.rb
epitools-0.5.126 lib/epitools/sys/os.rb
epitools-0.5.125 lib/epitools/sys/os.rb
epitools-0.5.124 lib/epitools/sys/os.rb
epitools-0.5.123 lib/epitools/sys/os.rb
epitools-0.5.122 lib/epitools/sys/os.rb
epitools-0.5.121 lib/epitools/sys/os.rb
epitools-0.5.119 lib/epitools/sys/os.rb
epitools-0.5.118 lib/epitools/sys/os.rb
epitools-0.5.116 lib/epitools/sys/os.rb
epitools-0.5.115 lib/epitools/sys/os.rb
epitools-0.5.114 lib/epitools/sys/os.rb
epitools-0.5.113 lib/epitools/sys/os.rb
epitools-0.5.112 lib/epitools/sys/os.rb