get system platform in ruby

To get the system platform in Ruby, you can use the RbConfig::CONFIG constant which contains information about the system configurations being used by Ruby, specifically RbConfig::CONFIG['host_os']. This returns a string representing the operating system name, version and architecture.

main.rb
platform = RbConfig::CONFIG['host_os']
puts platform
53 chars
3 lines

Alternatively, you can use the require 'rbconfig'; include RbConfig to include the RbConfig module at the beginning of your script and then call the CONFIG constant using the shorthand syntax CONFIG['host_os'].

main.rb
require 'rbconfig'
include RbConfig

platform = CONFIG['host_os']
puts platform
80 chars
6 lines

For more detailed system information, you can use the sys-uname gem, which provides an interface to the uname system call. This returns a Struct::Utsname object containing fields for the operating system name, version, release, and other system information.

main.rb
require 'sys/uname'

uname = Sys::Uname.uname
puts uname.sysname
puts uname.release
puts uname.version
puts uname.machine
122 chars
8 lines

You can also access environmental variables via the ENV object:

main.rb
puts ENV['OS']
15 chars
2 lines

gistlibby LogSnag