This tutorial will help you to interact with a Redis database using the Ruby programming language.
Step 1 – Install Redis Server
The first step is installing and setting up a Redis cluster on our local system. In this guide, we are using an Ubuntu 20.04 system. However, the commands should work on any Debian-based system.
Start by updating the system repository cache.
Next, Install Redis using the command:
Once the installation is complete, run the following command to verify the Redis server is installed.
You should see an output as shown:
Step 2 – Start Redis and Connect to the CLI
The next step is to start the Redis server. Using systemctl, enter the command:
The above command should start the Redis cluster and connect to the database.
Use the command below to connect to Redis using the CLI.
127.0.0.1:6379>
Once connected, you can begin to use Redis.
Step 3 – Installing Ruby
Ruby is a powerful programming language with tons of features for any modern developer. It offers an incredible syntax that is easy to read and understand. It is, therefore, an appropriate choice for interacting with Redis.
To use Ruby, we need to install it.
The simplest method to set up Ruby on our system is to use the APT package managers.
Start by updating the system repo cache.
Next, install Ruby with the command:
Once the command is complete, use the command below to verify Ruby is installed.
If installed, you should get an output as shown:
Step 4 – Using Ruby to Interact with Redis
After we have our development environment set up, we can use Ruby with Redis.
We need a Ruby gem developed to interact with Redis to do this. In this tutorial, we have chosen the redis-rb gem.
Use the gem command to install it.
Fetching redis-4.5.1.gem
Successfully installed redis-4.5.1
Parsing documentation for redis-4.5.1
Installing ri documentation for redis-4.5.1
Done installing documentation for redis after 0 seconds
1 gem installed
Once the gem is installed, we can start using it to interact with Redis.
Step 5 – Connect to Redis
Start by creating a ruby file.
Edit the file with your favorite text editor.
Add the code below to connect to your Redis cluster.
redis = Redis.new(host: "127.0.0.1", port: 6379, db: 0)
The code above imports the Redis package and creates a new connection. If you are using the default Redis configuration, you can omit the host, port, and db options.
Step 6 – Using Redis database with Ruby
To create a new key-value pair to a Redis database using Ruby, we can use the code as shown:
redis = Redis.new
redis.set("mykey", "myvalue")
redis.get("mykey")
The above code creates a new key and value using the set command. To fetch the value stored in a specific key, use the get method and pass the target key as the argument.
HINT: The redis-rb gem uses native Ruby commands to perform the operations.
Example 1
The following code uses Ruby to create a sorted set in Redis.
redis = Redis.new
redis.zadd("databases" 1, "MySQL")
redis.zadd("databases" 10, "FaunaDB")
redis.zadd("databases" 3, "Firestore")
redis.zadd("databases" 2, "MongoDB")
redis.zadd("databases" 5, "SQLite")
To get the elements in the sorted set, use the ZRANGE method.
Example 2
You can add lists to a Redis database using Ruby as:
redis = Redis.new
redis.lpush("langs", "Python")
To get the items in a list, use the LRANGE method as:
Closing
This guide demonstrates how to interact with a Redis database using the Ruby programming language. Check the redis-rb documentation to learn more.