Is a Ruby global variable equivalent to a class variable on Class Object?

Viewed 1089

I'm still fairly new to web development (well, all development), building an e-commerce app with Ruby on Rails for my porfolio. I'm using Redis for caching and to build a simple shopping cart (myself, not using a gem).

In addition to Rails, I've also stitched together a few simple apps using the MERN (Node, Express, Mongo, ReactJS) stack to boost my JavaScript skills.

In the course of copy-pasta'ing code for my Redis initializer (config/initializers/redis.rb), I observed some global variables:

if Rails.env.production?
  $redis = Redis.new(url: ENV["REDIS_URL"])
else
  $redis = Redis.new(:host => 'localhost', :port => 6379)
end

Because: JavaScript, I know that global variables make Baby Jesus cry or Mighty Zeus goes a big rubbery one or nothing, whatever (freedom of/from religion, etc)...

I know JavaScript and Ruby are vastly different animals:

1) JS: My understanding is that variables set outside the "outer" function's scope are global in context and store a location to the assigned value.

2) Ruby: $global_variable, @@class_variable, @instance_variable

3) "Everything" in Ruby is an object (except blocks and individual index items within an array), including classes, primitives, etc.

4) A Ruby Class, if it does not explicitly inherit from any another Class, inherits from the Object Class.

5) A Rails application's 'config/initialize' directory contains files that an instance of a Rails application should (wait for it) initialize:

Looking at my config/initializers/redis.rb file (code included above), the context is not explicit (i.e. there is no Class/Module/etc).

So...

6) config/environment.rb contains this code:

# Load the Rails application.
require_relative 'application'

# Initialize the Rails application.
Rails.application.initialize!

7) config/application.rb contains:

require_relative 'boot'
require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "action_cable/engine"
require "sprockets/railtie"
# require "rails/test_unit/railtie"

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module Ecom
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those 
specified here.
    # Application configuration should go into files in 
config/initializers
    # -- all .rb files in that directory are automatically loaded.
    config.cache_store = :redis_store, 
"redis://localhost:6379/0/cache", { expires_in: 90.minutes }
  end
end

8) Per the appropriate Rails Guide:
http://guides.rubyonrails.org/initialization.html#config-application

Rails.application.initialize!

This (wait for it) "initializes" an instance of the Rails app object.

9) The Rails.application object is an instance of Class Application (???)

Thank you for your time; given the above, here are my questions:

A) Is the Rails.application object actually an instance of the Application Class?

B) This was my original question, I included the above because I felt this simple question, without any context, might seem vague or unclear:

Is there any difference between a Ruby global variable:

$var = "A Ruby global variable"

and

Class Object
  @@var = "A class variable set on the Class Object"
end

I couldn't find this exact question addressed anywhere, my apologies if I missed something.

Are these functional equivalents, if not exact equivalents?

Is Class Object, as the Class from which all other Classes/Objects ultimately inherit, therefore, Ruby's "global" context (or not, and I missed something huge)?

C) (added as an edit) Because a global variable needs to be initiated and is available in a global scope, it is a class variable and not an instance variable. Is that reasoning sound?

Assuming I am correct in thinking that global variables and class variables are (at least) functionally equivalent, global variables are available "everywhere" because "everything" inherits from Class Object.

D) After considering the first comment, this might all have been better expressed along the chicken/egg paradigm?

If a global variable is an object, it has to inherit from Class Object, right? This would mean the "global" context is still wrapped within Class Object.

2 Answers
Related