Class: RubyChecker::Interpreter

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_checker/interpreter.rb

Overview

Interpreter adds checking capabilities for interpreters.

Instance Method Summary collapse

Constructor Details

#initialize(required) ⇒ Interpreter

Returns a new instance of Interpreter.



31
32
33
# File 'lib/ruby_checker/interpreter.rb', line 31

def initialize(required)
  @required = required
end

Instance Method Details

#check!Object

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.

Raises:



42
43
44
45
46
47
48
# 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_interpreterObject

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.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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_engineObject

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.



73
74
75
# File 'lib/ruby_checker/interpreter.rb', line 73

def ruby_engine
  defined?(RUBY_ENGINE) ? RUBY_ENGINE : nil
end

#stringify(interpreter) ⇒ Object

Returns a string representation for the given interpreter.



78
79
80
81
82
# 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