Use global or constant variable in Ruby/Rails?

ruby, ruby-on-rails

Solution

You might want to use `Redis.current` More info here

For example, in an initializer:

Redis.current = Redis.new(host: 'localhost', port: 6379)

And then in your other classes:

def stars
  redis.smembers("stars")
end

private

def redis
  Redis.current
end

Problem

Say we have a connection to memcache or redis... which style is preferred and why? ``` MEMCACHE = Memcache.new(...) REDIS = Redis.new(...) ``` OR ``` $memcache = Memcache.new(...) $redis = Redis.new(...) ```

Original source