Write code only for specific Ruby engine

02 May 2020

First, I try to use RUBY_PLATFORM like this:

if RUBY_PLATFORM = 'jruby'
  # code for jruby
else
  # code for ruby
end

But, it’s not working, then I do some debug on it:

# On official jruby docker image
puts RUBY_PLATFORM #=> x86_64-linux

# On Mac
puts RUBY_PLATFORM #=> x86_64-darwin19

So this variable is represent the platform of host.

Then, I foun RUBY_ENGINE:

# On official jruby docker image
puts RUBY_PLATFORM #=> jruby

# On Mac
puts RUBY_PLATFORM #=> ruby

That’s what I want.

if RUBY_ENGINE = 'jruby'
  # code for jruby
else
  # code for ruby
end
Back to top