get system cpu temperature in fahrenheit in ruby

To get the system CPU temperature in Fahrenheit in Ruby on a Linux-based system, you can use the sysfs virtual file system. Here's an example code snippet that accomplishes this task:

main.rb
def get_cpu_temperature_fahrenheit
  cpu_temp_celsius = File.read('/sys/class/thermal/thermal_zone0/temp').to_f / 1000.0
  cpu_temp_fahrenheit = (cpu_temp_celsius * 9/5) + 32
  return cpu_temp_fahrenheit.round(2)
end

puts "CPU Temperature: #{get_cpu_temperature_fahrenheit} °F"
279 chars
8 lines

This code reads the current CPU temperature in Celsius from the virtual file system at /sys/class/thermal/thermal_zone0/temp. It then converts the temperature to Fahrenheit and rounds the result to two decimal places. Finally, the temperature is printed out as a string using string interpolation with units of Fahrenheit (°F).

gistlibby LogSnag