class RubyChecker::Interpreter

Interpreter adds checking capabilities for interpreters.

Public Class Methods

new(required) click to toggle source
# File lib/ruby_checker/interpreter.rb, line 31
def initialize(required)
  @required = required
end

Public Instance Methods

check!() click to toggle source

check! performs checks for interpreter, ruby engines, etc. It might raise the following exceptions:

  • NotSupportedError: if the current interpreter does not match with the expectation.

  • RubyEngineNotAvailableError: if the RUBY_ENGINE constant has not been defined.

# File lib/ruby_checker/interpreter.rb, line 42
def check!
  return true if @required == ANY

  raise NotSupportedError, stringify(@required) if current_interpreter != @required

  true
end
current_interpreter() click to toggle source

Returns the constant representing the current ruby interpreter. It will return nil if the interpreter is not supported by this gem and it will raise a RubyEngineNotAvailableError if the RUBY_ENGINE constant has not been defined.

# File lib/ruby_checker/interpreter.rb, line 54
def current_interpreter
  raise RubyEngineNotAvailableError if ruby_engine.nil?

  case ruby_engine
  when "ruby"
    MRI
  when "jruby"
    JRUBY
  when "mruby"
    MRUBY
  when "truffleruby"
    TRUFFLE
  end
end
ruby_engine() click to toggle source

ruby_engine returns the value for RUBY_ENGINE, or nil if this constant is not defined.

This method lives mainly so it can be stubbed in tests.

# File lib/ruby_checker/interpreter.rb, line 73
def ruby_engine
  defined?(RUBY_ENGINE) ? RUBY_ENGINE : nil
end
stringify(interpreter) click to toggle source

Returns a string representation for the given interpreter.

# File lib/ruby_checker/interpreter.rb, line 78
def stringify(interpreter)
  return "unknown interpreter" unless interpreter.is_a?(Integer)

  %w[MRI JRuby mruby truffleruby][interpreter - 1] || "unknown interpreter"
end