Search code examples
rubyglobal-variablesrubymine

RubyMine: Unresolved Ruby Reference for global variable


Running RubyMine 2016.2.4. This project has a "global" singleton called $environment. All over the codebase this file is require_relative'd to get access to runtime dependencies. RubyMine is showing "Unresolved Ruby Reference" for every instance of $environment in classes which use this global.

Mock-up of how the environment.rb file looks:

require 'singleton'

class Environment
  include Singleton
  def log
    ...
  end
end

$environment ||= Environment.instance

Example usage which RubyMine complains about:

require_relative '../environment'

class FancyWorker
  def run
    ...
    $environment.log.info 'Running!'
  end
end

I've tried searching to no avail on how to resolve this inspection problem in RubyMine. I don't want to disable the inspection since it is useful for finding legitimate problems - but this is not a legitimate issue.

(Please keep commentary about globals being bad to yourself, I didn't make that design decision - I just want to help RubyMine understand the reference.)


Solution

  • I think you found a bug on RubyMine. Try changing:

    $environment ||= Environment.instance
    

    to:

    $environment = $environment || Environment.instance
    

    or just assign it if you're sure you won't be defining $environment anywhere else.