Ruby Connection Examples ======================== .. meta:: :description: Example code for connecting to Redis in Ruby with redis-rb .. |checkmark| unicode:: U+2713 The `redis-rb `_ gem is the recommended client for Redis when using Ruby. Installation ------------ Install redis-rb at the command prompt if you haven't yet: .. code-block:: bash $ gem install redis Connecting with SSL --------------------- Connecting to your instance via SSL requires using a certificate authority. See documentation for `certificates and fingerprints `_ for more details. .. code-block:: ruby require 'redis' begin redis = Redis.new( :host => '#####.publb.rackspaceclouddb.com', :port => 6380, :password => 'YOUR_PASSWORD', :ssl => :true, :ssl_params => { :ca_file => 'LOCAL/PATH/TO/rackspace-ca-2016.pem' } ) puts redis.info client_ping = redis.ping if client_ping puts 'Connected!' else raise 'Ping failed' end rescue => e puts "Error: #{e}" end Connecting without SSL ----------------------- .. code-block:: ruby require 'redis' begin redis = Redis.new( :host => '#####.publb.rackspaceclouddb.com', :port => 6379, :password => 'YOUR_PASSWORD' ) puts redis.info client_ping = redis.ping if client_ping puts 'Connected!' else raise 'Ping failed' end rescue => e puts "Error: #{e}" end Creating, Reading, Updating and Deleting Records ------------------------------------------------- .. code-block:: ruby require 'redis' begin redis = Redis.new( :host => '#####.publb.rackspaceclouddb.com', :port => 6379, :password => 'YOUR_PASSWORD' ) puts 'Create Record:', redis.set('best_car_ever', 'Tesla Model S') puts 'Get Record:', redis.get('best_car_ever') puts 'Delete Record:', redis.del('best_car_ever') puts 'Get Deleted Record:', redis.get('best_car_ever') rescue => e puts "Error: #{e}" end Output from above: .. code-block:: ruby Create Record: OK Get Record: Tesla Model S Delete Record: 1 Get Deleted Record: More Information ---------------- If you need additional help with redis-rb, here are some useful links: * `redis-rb Official Documentation `_ As always, if you have any questions, please don't hesitate to reach out to our `support team `_!