title: Ruby on Rails Global Class Variable not updating when changed
body: <p>I want to add a slight extension to a class in a ruby gem.</p>
  
  <p>The gem I am using is private_pub : <a href="https://github.com/ryanb/private_pub" rel="nofollow">https://github.com/ryanb/private_pub</a> and the class I am overriding is : <a href="https://github.com/ryanb/private_pub/blob/master/lib/private_pub/faye_extension.rb" rel="nofollow">https://github.com/ryanb/private_pub/blob/master/lib/private_pub/faye_extension.rb</a></p>
  
  <p>I am have placed my implementation in a new file in config/initializers called faye_extension.rb. The piece I have added to the code is @@clients and the get_list_of_subscribers class method. The code in faye_extension.rb is as follows:</p>
  
  <pre><code>PrivatePub::FayeExtension
  module PrivatePub
    # This class is an extension for the Faye::RackAdapter.
    # It is used inside of PrivatePub.faye_app.
    class FayeExtension
      # Callback to handle incoming Faye messages. This authenticates both
      # subscribe and publish calls.
  
  #####  MY NEW BIT
      @@clients = 0
      def self.get_list_of_subscribers
        @@clients
      end
  
  ####
  
  
      def incoming(message, callback)
      @@clients = @@clients + 1
        if message["channel"] == "/meta/subscribe"
          authenticate_subscribe(message)
        elsif message["channel"] !~ %r{^/meta/}
          authenticate_publish(message)
        end
        callback.call(message)
      end
  
    private
  
      # Ensure the subscription signature is correct and that it has not expired.
      def authenticate_subscribe(message)
        subscription = PrivatePub.subscription(:channel =&gt; message["subscription"], :timestamp =&gt; message["ext"]["private_pub_timestamp"])
        if message["ext"]["private_pub_signature"] != subscription[:signature]
          message["error"] = "Incorrect signature."
        elsif PrivatePub.signature_expired? message["ext"]["private_pub_timestamp"].to_i
          message["error"] = "Signature has expired."
        end
      end
  
      # Ensures the secret token is correct before publishing.
      def authenticate_publish(message)
        if PrivatePub.config[:secret_token].nil?
          raise Error, "No secret_token config set, ensure private_pub.yml is loaded properly."
        elsif message["ext"]["private_pub_token"] != PrivatePub.config[:secret_token]
          message["error"] = "Incorrect token."
        else
          message["ext"]["private_pub_token"] = nil
        end
      end
    end
  end
  </code></pre>
  
  <p>When deployed into my application, the incoming() method of the FayeExtension code is called multiple times, however if in a view somewhere I use the following line of code:</p>
  
  <pre><code>&lt;h3&gt;&lt;%= PrivatePub::FayeExtension.get_list_of_subscribers.to_s %&gt;&lt;/h3&gt;
  </code></pre>
  
  <p>to call my get_list_of_subscribers class method, it always returns 0, despite me calling incoming() 5 times, where I would expect it to output 5. So it seems my <code>@@clients = @@clients +1</code> code inside of incoming() is not referencing or updating correctly my global variable.</p>
  
  <p>How can I alter the code to achieve this?</p>