Marketplace has launched, further enabling blockchain developers! Learn more

How to Connect to the Ethereum Network using Ruby

September 23, 2022

Overview

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!

Prerequisites
  • 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:

ruby -v

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, 7-day trial. First, create an 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:

A screenshot of the Quicknode endpoint Getting Started page with HTTP link and WSS


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:

putting it all together

Copy
require 'eth'
client = Eth::Client.create 'YOUR_ETHEREUM_NODE_URL'
block_number = client.eth_block_number
puts block_number["result"].to_i(16)

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:

ruby script.rb

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.

Conclusion

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 :)

Related articles 17

Pending and Queued Transactions Explained
Published: Mar 18, 2022
Updated: Sep 23, 2022

The Ethereum network processes over one million transactions a day. These transactions originate from users wanting to transfer Ether or interact with smart contracts. In this guide, we will...

Continue reading
What is Ethereum?
Published: Sep 23, 2022
Updated: Sep 26, 2022

Ethereum is a decentralized and open-sourced blockchain network that allows anyone in the world to participate and interact with it. In this guide, you will learn what Ethereum is, how it...

Continue reading
What are Ethereum Transactions?
Published: Mar 28, 2022
Updated: Sep 23, 2022

Transactions in Ethereum are cryptographically signed data messages that contain a set of instructions. These instructions can interpret to sending Ether from one Ethereum account to another...

Continue reading
How To Fork Ethereum Mainnet with Hardhat
Published: Jul 13, 2021
Updated: Oct 18, 2022

Forking the chain at an older block of the blockchain is helpful if you want to simulate the blockchain’s state at that block; Hardhat has this functionality built in. In this guide, let’s go...

Continue reading
How to Send an EIP-1559 Transaction
Published: Dec 3, 2021
Updated: Sep 23, 2022

While Ethereum has been trying to scale, it has encountered some gas price issues. Many layer 2 solutions and sidechains sprang into existence to solve this problem, but Ethereum is the main...

Continue reading