7

I need to determine what environment my ruby script is running in so I can remove files and clean up directories after execution.

I tried using ENV['os'] but I am using cygwin and it gave me Windows_NT, Does anyone know a way to find the current environment?

Thanks

Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
  • Duplicate of https://stackoverflow.com/q/170956/5389585 – sondra.kinsey Mar 14 '19 at 17:56
  • Possible duplicate of [How can I find which operating system my Ruby program is running on?](https://stackoverflow.com/questions/170956/how-can-i-find-which-operating-system-my-ruby-program-is-running-on) – sondra.kinsey Mar 14 '19 at 17:56

3 Answers3

8

The current environment is provided by the global constant RUBY_PLATFORM

ruby invoked at the cygwin bash shell (/usr/bin/ruby):

puts RUBY_PLATFORM
i386-cygwin

ruby invoked at the command prompt (c:\Ruby193\bin\ruby.exe):

puts RUBY_PLATFORM
i386-mingw32

puts ENV['OS'] for both of the above environment returns: Windows_NT

nick1176
  • 81
  • 1
  • 2
3

The current os can be obtained from RUBY_PLATFORM

puts RUBY_PLATFORM
X86_64-linux

However, when I use ENV['os'] on Linux machine, Ruby returns nil.

euccas
  • 755
  • 9
  • 13
1

I've used the gem 'OS' which can be found here: http://rubygems.org/gems/os

I haven't tried it with cygwin, but I did replace my own use of the ENV variable hash with it.

Ryanmt
  • 3,215
  • 3
  • 22
  • 23
  • This question also addresses your problem. http://stackoverflow.com/questions/170956/ruby-how-can-i-find-out-on-which-system-my-program-is-running – Ryanmt Jun 29 '11 at 15:48
  • os gem works beautifully. We can do things like `OS::windows?` `OS::linux?` – rpattabi Sep 11 '12 at 14:26