4 min read
The Ruby programming language has a huge fanbase. Ruby was developed by its creator with an intention to invent a language developers can enjoy learning and using. Ruby has been largely accepted by developers all around the world since its launch, in fact, the biggest tech communities in many cities consist of Ruby developers. In this guide, we will learn how to connect to the Ethereum network using a Ruby gem (or package) called eth.rb .
By using eth.rb, we can make direct JSON RPC calls to a node from any Ruby application. eth.rb also boasts numerous other functionalities listed in their official Github repo:
"...to build, sign, and broadcast Ethereum transactions. It allows the separation of key and node management. Sign transactions and handle keys anywhere you can run Ruby and broadcast transactions through any local or remote node. Sign messages and recover signatures for authentication." Source
In this guide, we'll take a closer look into how we can install and interact with this package by setting it up locally on our machine and then retrieving the block height from our node. Let's get started!
- Ruby version >= 2.6, < 4.0
- A code editor
- Terminal aka Command Line
Installing the Eth Gem
Before installing the gem, let us first ensure that Ruby is installed. Simply open a terminal and run:
If this returns a version later than 2.6, you're all set! If the command is not recognized, then you will need to install Ruby. If it is and the version is older than 2.6, you will need to use a newer one. Note: the version of Ruby that ships with macOS is usually for Apple's own use and it is best not to change it. You can make changes to this version, however, instead, we suggest using rbenv or RVM (Ruby Version Manager) to manage a separate Ruby version, which will be installed into a sandbox in your home directory. You can make changes to this version without worrying about changing the system's Ruby version. For more information, read this external guide from mac.install.
Once you're ready to move on, we can install the eth gem. This gem will allow us to connect to the Ethereum blockchain network using the Ruby language. We can install it from the command line using the RubyGems package manager:
gem install eth
Getting Started with QuickNode
To build on Ethereum, you'll need an API endpoint to talk to on their network. If you'd like to deploy, host, and manage your own infrastructure, you can skip this section. If you'd like to leave the heavy lifting to us, you can sign up for a free account on QuickNode.com.
Create an Endpoint
Once you’ve signed up, create an endpoint running on the Ethereum network. Then, navigate to the "Get Started" tab and copy the HTTP Provider link:
We'll use this to connect to the Ethereum network.
Putting It All Together
Now, we'll use the eth gem along with our QuickNode endpoint to create a short script to fetch the latest block number using our node.
Create a new file, script.rb, through your Terminal or directly in your file system. If you choose to use a Terminal, you can use this command:
echo > script.rb
Open script.rb in a code editor of choice and add the following code:
client = Eth::Client.create 'YOUR_ETHEREUM_NODE_URL'
block_number = client.eth_block_number
Replace the YOUR_ETHEREUM_NODE_URL with the HTTP provider from the instructions above.
Let us break down the code:
- Line 1: We are importing the eth gem we installed earlier.
- Line 3: We are creating a new Ethereum RPC client, passing in our Ethereum node URL. Visit the official eth.rb Github repo for more information.
- Line 4: We are getting the latest Ethereum block number using the eth_block_number method and storing it in block_number.
- Line 5: We are printing the block number. Note that the returned result is in hexadecimal format, thus we use the to_i(16) function to convert it into an integer, base 16.
Run the Script
Execute the script by running the following in your Terminal:
After running this command, you will see the latest Ethereum block number returned on the following line. That’s it! We've successfully connected to the Ethereum network using Ruby.
Subscribe to our newsletter for more articles and guides. Feel free to reach out to us via Twitter if you have any feedback. You can always chat with us on our Discord community server, featuring some of the coolest developers you'll ever meet :)
We ❤️ Feedback!
Let us know if you have any feedback or requests for new topics. We'd love to hear from you.