DEV Community

Germán Alberto Gimenez Silva
Germán Alberto Gimenez Silva

Posted on • Originally published at rubystacknews.com on

🔍 Exploring Ruby’s Hidden Powers: Getting Full System Info with Pure Ruby

🔍 Exploring Ruby’s Hidden Powers: Getting Full System Info with Pure Ruby
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

Article content

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

Enter fullscreen mode Exit fullscreen mode

CopyEdit

puts ENV[‘HOME’] # => “/home/youruser” puts ENV[‘PATH’] # => Colon-separated system paths

Or loop through all:


ENV.each { |k, v| puts "#{k} = #{v}" }

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

A classic pattern:


if __FILE__ == $0
  puts "Running directly!"
end

Enter fullscreen mode Exit fullscreen mode

🧰 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']

Enter fullscreen mode Exit fullscreen mode

Or even user-level info:


require 'etc'
puts Etc.getlogin
puts Etc.getpwuid.dir # Home directory

Enter fullscreen mode Exit fullscreen mode

🌐 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)

Enter fullscreen mode Exit fullscreen mode

🧪 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.

Article content


🙌 If you’re a Rubyist—or curious about scripting and introspection—I’d love to hear how you’re using these features!

ruby #cli #developers #rubyonrails #introspection #opensource #programmingtips #environmentvariables

Top comments (0)