How can I tell if I'm running from JRuby vs. Ruby?

pastorius picture pastorius · Aug 27, 2009 · Viewed 9.2k times · Source

I have a script with a factory method that I would like to return a different implementation of some class depending on whether or not the script is running from JRuby or Ruby. Anyone have any ideas on how I could tell the difference from inside my script?

Some initial thoughts I had were:

  • Attempt to 'include Java' and rescue back to the Ruby implementation if it fails. This method doesn't work. Ruby's smart enough to error out regardless of my begin/rescue/end.

  • Do something goofy with process IDs. I'd rather avoid this, since it always feels like a hack.

Answer

JesperE picture JesperE · Aug 27, 2009

I believe you can check the RUBY_PLATFORM constant.