March 20, 2021

How to connect to the Ethereum network using Ruby

Overview

Ruby has a huge fanbase. Ruby was developed by its creator with an intention to create a language people can have fun using. Ruby has been largely accepted by the developers all around the world since it’s launch, in fact, the biggest tech communities in many cities are ruby communities. In this article, we will learn how to connect to ethereum network using Ruby, ethereum.rb is a package which can be used to connect to ethereum blockchain network and help us easily accomplish meaningful things.

Fun fact: Ruby packages are called gems.

By using ethereum.rb you can make direct JSON RPC calls to the node from any ruby application, it has simple syntax, you can compile Solidity contracts with solc compiler from ruby, receive events from contract, and is programmer-friendly. You can also sign Ethereum transactions using another Ruby gem ruby-eth which allows separation of key and node management. Sign transactions and handle keys anywhere you can run ruby, broadcast transactions through any node.

Note: Currently ethereum.rb supports only parity node. 

Prerequisites
  • Ruby installed in your system(Ruby 2.x +)
  • A text editor
  • Terminal aka Command Line

Installing the ethereum.rb gem

Before installing the gem let's make sure that ruby is installed. Simply open a terminal and run:

$ ruby -v

Once we ensure ruby is installed and at the right version (2.0+), let's move forward and install the ethereum.rb gem which will help us connect to ethereum blockchain network using ruby language, You can install it from the command line using RubyGems the package manager for ruby:

$ gem install ethereum.rb

If you're using macOS you may encounter a permission-related problem while installing gem that is because the version of Ruby that ships with macOS is usually for Apple's own use, you can always make few changes and get going but I won't recommend that.

Instead of that, I'll 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, that you can make changes to without worrying about messing up the system Ruby. 

Booting our Ethereum node

For our purposes today, we would have to use OpenEthereum (fka Parity) because ethereum.rb only works with OpenEthereum. Since that is a bit too involved for just querying for block height, we'll just grab a free endpoint from QuikNode to make this easy. After you've created your free ethereum endpoint, copy your HTTP Provider endpoint:



You'll need this later, so copy it and save it.

Connecting via ethereum.rb

Now let's create a short script, let's call it `index.rb`, to fetch the block height from our node. You can copy/paste this into your code editor:

require 'ethereum.rb'
client = Ethereum::HttpClient.new('YOUR_ETHEREUM_NODE_URL')
blockNumber = client.eth_block_number
puts blockNumber["result"].to_i(16)

So go ahead and replace `ADD_YOUR_ETHEREUM_NODE_URL` with the http provider from the instructions above.

A quick explanation of the code above - we are importing the ethereum.rb gem we installed earlier (line 1), setting our Ethereum node URL (line 2), we are getting the latest Ethereum block number using client.eth_block_number API (line 3)and Printing the block number but as the API returns the block number in hexadecimal, we will convert it in decimal using to_i(16) function (lines 4).

Save this code snippet in a file index.rb we're going to run this very shortly.

Confirm it's working

Run the file by running the following in a terminal:

$ ruby index.rb

After running this command we will see the latest Ethereum block number as shown below.


That’s it we have connected through Ethereum network using ethereum.rb gem for Ruby. In addition, also check out other Ethereum library in ruby, ruby-eth.

Subscribe to our newsletter for more articles and guide on Ethereum. If you have any feedback, please feel free to reach out to us via Twitter and you can always chat with us if you have a question via our community server on Discord, thanks :)
Ready to try QuickNode? Start your 7-day free trial

Related articles 16

Mar 20, 2021 How to connect to Ethereum using .NET (Nethereum)

Dotnet or .NET is very popular for the development of desktop applications, most Windows desktop applications are built using .NET, and it also contributes largely to web application’s tech stack. In this guide, let’s see how we can connect to Ethereum using .NET and

Continue reading
Mar 20, 2021 How to fetch Ethereum event logs in Ruby

Ethereum log records are very useful to understand and keep track of smart contract events. In this guide, we are going to learn how to fetch ethereum event logs in Ruby using ethereum.rb ruby...

Continue reading
Mar 20, 2021 How to generate a new Ethereum address in Go

Golang is very popular among backend developers for building infrastructures and microservices. Go is a procedural programming language. Developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google, then launched in 2009 as...

Continue reading
Mar 20, 2021 How to generate a new Ethereum address in Python

Python is one of the most versatile programming languages out there with an abundance of use cases; We can build many applications with Python from client-side to back end. In this guide, we will cover creating an Ethereum address in Python using the

Continue reading
Mar 20, 2021 How to generate a new Ethereum address in JavaScript

When it comes to programming, there’s hardly anyone who has not used or heard about JavaScript. JavaScript was initially created for client-side scripting but has become a full-featured Object-Oriented...

Continue reading
Mar 20, 2021 How to connect to the Ethereum network using Python using Web3.py

You can build Ethereum applications in different programming languages. In this article, we will connect to the Ethereum network using Python.PrerequisiteEthereum Node (We will use QuikNode’s free...

Continue reading
Mar 20, 2021 How to connect to Ethereum network with ethers.js

When someone thinks of developing a dApp the first tool that comes to their mind is web3.js which is pretty common because of its popularity in the community and wide use cases, dApp development has been consistently growing and there are a lot of developers who want to...

Continue reading
Mar 20, 2021 How to re-send a transaction with higher gas price using ethers.js

Sometimes, you submit a transaction on Ethereum without enough gas due to network congestion or too many pending transactions offering a higher gas price than you have offered on your transaction. If you have a high priority transaction but low gas, you could end up...

Continue reading
Mar 20, 2021 How to connect to Ethereum network with Web3.js

Libraries and frameworks make the development process a lot easier and faster. When it comes to Ethereum development, Web3.js is the go to library. That's because Web3.js is the official library, from the

Continue reading
Mar 20, 2021 How to use Subspace with QuikNode

In this guide, we'll understand a bit about reactive development and how to use Subspace with QuikNode.JavaScript is the programming language behind most of the internet apps and websites. JavaScript today has become one of the most used programming languages, and...

Continue reading
Mar 20, 2021 How to generate a new Ethereum address in Ruby

With high usage in web applications and straightforward syntax, Ruby is used by a vast number of people. This guide will cover creating an Ethereum address in Ruby using ruby-eth...

Continue reading
Mar 20, 2021 How to connect to Ethereum network using Java / Web3j

We can say that Java is one of the most versatile languages out there, and it continues to be relevant in today's time. Java is so popular because of its massive user base and use cases. In this guide/tutorial, we'll learn how to connect to the Ethereum Blockchain network...

Continue reading
Mar 31, 2021 How to generate a new Ethereum address in PHP

PHP is very popular in developing the backend of websites or web applications. PHP has a huge crowd of developers trusting it as their go-to language. In this guide, we will see how we can generate a new Ethereum address in...

Continue reading
Apr 13, 2021 How to Fork Ethereum Blockchain with Ganache.

Forking and running a local simulated Ethereum environment is essential if you want to work with DeFi or do Ethereum development in general. In this guide, we’ll cover how to fork Ethereum Blockchain with

Continue reading
Mar 20, 2021 Estimating gas price using pending transactions in Python

To send a transaction on the Ethereum network, you need to pay fees for including the transaction in a block as well as the computation necessary in the transaction; this fee is called gas. The transactions are accepted into the block based on the amount of gas they are...

Continue reading
Mar 20, 2021 How to connect to Ethereum network using Go

Go helps you make faster scalable backends and this guide will show you how to connect your backend to Ethereum (and make it even faster, more reliable, and globally accessible, all thanks to QuikNode’s global infrastructure). What is...

Continue reading