Ruby – How to tell if I’m running from JRuby vs. Ruby

jrubyruby

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.

Best Answer

I believe you can check the RUBY_PLATFORM constant.