public
Last active

  • Download Gist
cpu_count.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13
module System
extend self
def cpu_count
return Java::Java.lang.Runtime.getRuntime.availableProcessors if defined? Java::Java
return File.read('/proc/cpuinfo').scan(/^processor\s*:/).size if File.exist? '/proc/cpuinfo'
require 'win32ole'
WIN32OLE.connect("winmgmts://").ExecQuery("select * from Win32_ComputerSystem").NumberOfProcessors
rescue LoadError
Integer `sysctl -n hw.ncpu 2>/dev/null` rescue 1
end
end
 
System.cpu_count # => 2

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.