As of Ruby version 2.2.3, the etc
module in Ruby’s stdlib offers an nprocessors
method which returns the number of processors. The caveat to this, is that if ruby is relegated to a subset of CPU cores, Etc.nprocessors
will only return the number of cores that Ruby has access to. Also, as seanlinsley pointed out, this will only return virtual cores instead of physical cores, which may result in a disparity in the expected value.
require 'etc'
p Etc.nprocessors #=> 4