module CPUInfo class CPUS def self.cpus @@cpus ||= self.parse_cpuinfo end def self.[](*parameters) (self.cpus).public_send(:[], *parameters) end def to_s self.cpus.map(&:to_s).join("\n\n") end def self.method_missing(sym, *parameters) self.cpus.public_send(sym, *parameters) if self.cpus.respond_to? sym end class CPU attr_reader :attributes def initialize(cpuinfo) @attributes = {} cpuinfo.lines.each do |line| matches = /^([^:]+):(.+)\n/.match(line) next unless matches attribute = matches[1].strip.gsub(/[^a-zA-Z_]/,'_') value = matches[2].strip if value =~ /^[0-9]+(\.0*)?$/ value = value.to_i elsif value =~ /^[0-9\.]$/ value = value.to_f elsif ['no', 'false'].include? value.downcase value = false elsif ['yes', 'true'].include? value.downcase value = true elsif attribute == 'flags' value = CPUFlags.new value end @attributes[attribute.to_sym] = value end end def method_missing(sym, *parameters) return @attributes[sym.to_sym] if parameters.empty? && @attributes.key?(sym.to_sym) @attributes[:flags].public_send(sym, *parameters) end class CPUFlags attr_reader :flags def initialize(flagstring) @flags = flagstring.split(' ') end def method_missing(method, *params) if method[-1] == '?' return @flags.include? method[0..-2] end end end end def self.read_cpuinfo # IO.read("/proc/cpuinfo") end def self.parse_cpuinfo self.read_cpuinfo.split(/^processor:\w+\d+:\w+\n/).map {|cpu| CPU.new(cpu) } end end end