get system release in ruby

To get the system release information in Ruby, you can use the RbConfig::CONFIG hash to access the host_os key which contains the operating system name and version. You can then use a regular expression to extract the release information as follows:

main.rb
os = RbConfig::CONFIG['host_os']
case os
when /linux/
  release = `lsb_release -a 2>/dev/null | grep Release | cut -f2`.chomp
when /darwin|mac os/
  release = `sw_vers -productVersion`.chomp
when /mswin|mingw|cygwin/
  if Gem.win_platform?
    release = `ver`.chomp
  else
    raise "Unsupported Windows platform: #{os.inspect}"
  end
else
  raise "Unsupported operating system: #{os.inspect}"
end

puts "System Release: #{release}"
433 chars
18 lines

This code snippet uses a case statement to determine the operating system and execute the appropriate command to obtain the release information. For Linux, it runs the lsb_release command and extracts the Release information. For macOS, it runs the sw_vers command and extracts the productVersion information. For Windows, the ver command is used.

gistlibby LogSnag