DEV Community

Cover image for Ruby & blockchain
Davide Santangelo
Davide Santangelo

Posted on

Ruby & blockchain

A blockchain is a decentralized and distributed digital ledger that is used to record transactions across a network of computers. It is a type of distributed database that is composed of blocks of data that are linked and secured using cryptography.

Here is a simple example of a blockchain implemented in Ruby:

# This class represents a block in the blockchain
class Block
  attr_reader :data, :timestamp, :prev_hash, :hash

  def initialize(data, prev_hash = nil)
    @data = data
    @timestamp =
    @prev_hash = prev_hash
    @hash = calculate_hash

  def calculate_hash

# This class represents the blockchain
class Blockchain
  attr_reader :chain

  def initialize
    @chain = [genesis_block]

  def genesis_block"This is the first block in the chain.", "0")

  def last_block

  def add_block(data)
    new_block =, last_block.hash)
    @chain << new_block

  def valid?
    (1...@chain.length).all? do |i|
      @chain[i].prev_hash == @chain[i - 1].hash
Enter fullscreen mode Exit fullscreen mode

In this code, the Block class represents a single block in the blockchain. Each block has data, a timestamp, a hash, and a reference to the previous block's hash. The hash of a block is calculated using the SHA-256 cryptographic hash function, which takes the previous block's hash, the block's timestamp, and its data as inputs.

The Blockchain class represents the entire blockchain. It maintains a list of blocks in the @chain instance variable, and it provides methods for adding new blocks to the chain, checking the validity of the chain, and accessing the last block in the chain.

To create a new blockchain, you would simply create a new instance of the Blockchain class:

# Create a new blockchain
blockchain =
Enter fullscreen mode Exit fullscreen mode

To add a new block to the blockchain, you would use the add_block method:

# Add a new block to the blockchain
blockchain.add_block("This is the second block in the chain.")
Enter fullscreen mode Exit fullscreen mode

To check the validity of the blockchain, you would use the valid? method:

# Check if the blockchain is valid
if blockchain.valid?
  puts "The blockchain is valid."
  puts "The blockchain is not valid."
Enter fullscreen mode Exit fullscreen mode

Here are some tests that you can use to verify the functionality of the Ruby blockchain code that was provided in the previous answer:

# Test the Block class
describe Block do
  let(:block) {"test data") }

  it "has data" do
    expect( eq "test data"

  it "has a timestamp" do
    expect(block.timestamp).to be_a Time

  it "has a previous hash" do
    expect(block.prev_hash).to be_nil

  it "has a hash" do
    expect(block.hash).to be_a String

# Test the Blockchain class
describe Blockchain do
  let(:blockchain) { }

  it "has a chain" do
    expect(blockchain.chain).to be_an Array

  it "has a genesis block" do
    expect(blockchain.chain[0].data).to eq "This is the first block in the chain."
    expect(blockchain.chain[0].prev_hash).to eq "0"

  it "can add blocks" do
    blockchain.add_block("test data")
    expect(blockchain.chain.length).to eq 2

  it "can check the validity of the chain" do
    blockchain.add_block("test data")
    expect(blockchain.valid?).to be true
Enter fullscreen mode Exit fullscreen mode

These tests use the rspec testing framework, which is a popular choice for testing Ruby code. The tests are written in a descriptive style, using the describe and it methods to define the tests and specify their expected behavior.

To run these tests, you would need to have the rspec gem installed on your system. You can then run the tests by typing the following command at the command line:

rspec blockchain_tests.rb
Enter fullscreen mode Exit fullscreen mode

This will run the tests and output the results to the terminal. If all of the tests pass, then you should see a message indicating that all of the tests passed. If any of the tests fail, then you will see a detailed error message that indicates which test failed and why. You can use this information to debug your code and fix any issues that are preventing the tests from passing.

Here are some links that you can use to learn more about blockchain:

Blockchain Basics
Introduction to Blockchain
A Beginner's Guide to Blockchain
Blockchain for Dummies
The Ultimate Guide to Blockchain Development
The Blockchain Academy

These resources provide a range of information about Ruby, blockchain, and related topics. You can use them to learn more about the basics of Ruby, how to use it to build web applications

Top comments (2)

pedrogrande profile image
Pete Argent

Hi there. Just letting you know that all the links at the bottom are broken/dead.

q9 profile image

And now p2p-networking :D