Exploring Ruby’s Hidden Powers: Getting Full System Info with Pure Ruby
June 30, 2025
As developers, we often turn to external tools or system commands to gather environment and system information. But did you know that Ruby gives you everything you need—natively—to inspect the full context in which your code is running?
Recently, I dove into how Ruby offers access to environment variables, file system paths, user data, runtime configs, and more —all with built-in tools. Here’s a breakdown of what I found most useful and how you can use them in your own work.
Accessing Environment Variables
Ruby gives you full access to the system’s environment through the ENV hash:
puts ENV['HOME'] # => "/home/youruser"
puts ENV['PATH'] # => Colon-separated system paths
CopyEdit
puts ENV[‘HOME’] # => “/home/youruser” puts ENV[‘PATH’] # => Colon-separated system paths
Or loop through all:
ENV.each { |k, v| puts "#{k} = #{v}" }
CopyEdit
ENV.each { |k, v| puts “#{k} = #{v}” }
Real Paths and File Context
Need to know where your code is running or resolve symlinks? Ruby’s File and Pathname classes have you covered:
puts File.realpath( __FILE__ ) # Canonical file path
puts File.expand_path('.') # Full current directory
puts Pathname.new( __FILE__ ).dirname # Clean directory path
These are especially useful for CLI tools, config loaders, and test frameworks.
Know Your Execution Context
Ruby provides special constants and global variables to let you know how a file was executed:
__FILE__ # Current file name
__LINE__ # Current line number
$0 # Script name (entry point)
$$ # Current process ID
A classic pattern:
if __FILE__ == $0
puts "Running directly!"
end
Dive into Ruby and OS Configuration
Need deeper insights about the Ruby build or OS platform?
require 'rbconfig'
puts RbConfig::CONFIG['host_os'] # e.g. linux-gnu
puts RbConfig::CONFIG['ruby_version']
Or even user-level info:
require 'etc'
puts Etc.getlogin
puts Etc.getpwuid.dir # Home directory
Host and Network Details
Need IPs or hostname for debugging or logging?
require 'socket'
puts Socket.gethostname
puts Addrinfo.getaddrinfo(Socket.gethostname, nil, :INET).map(&:ip_address)
Ruby as a System Inspector
Put it all together and you can build your own diagnostic tool in pure Ruby. No gems, no shell commands—just Ruby magic. This is perfect for cross-platform CLI tools or debug reports in distributed systems.
Final Thoughts
As developers, it’s easy to forget just how powerful our languages are. Ruby not only gives you beautiful syntax but also deep introspection into the environment it’s running in.
Whether you’re building CLIs, automated scripts, or just want to better understand how your app runs in different environments— Ruby gives you the keys to the kingdom.
If you’re a Rubyist—or curious about scripting and introspection—I’d love to hear how you’re using these features!
Top comments (0)