Rubidity by Example

Let's start with some examples from Solidity's introduction.

Storage Example

This basic Solidity contract sets the value of a variable and then exposes it to others to access:

pragma solidity >=0.4.16 <0.9.0;

contract SimpleStorage {
    uint storedData;

    function set(uint x) public {
        storedData = x;
    }

    function get() public view returns (uint) {
        return storedData;
    }
}

Here is how this logic translates to Rubidity:

pragma :rubidity, "1.0.0"

contract :SimpleStorage do
  uint256 :storedData

  constructor() {}
  
  function :set, { x: :uint256 }, :public do
    s.storedData = x;
  end

  function :get, :public, :view, returns: :uint256 do
    return s.storedData
  end
end

Looking point by point, here's what the Solidity version does and how Rubidity translates it.

Contract Definition

  • Solidity: contract SimpleStorage { ... }

  • Rubidity: contract :SimpleStorage

    In Solidity, you define a contract using the contract keyword. In Rubidity, you define a contract using the contract method.

State Variables

  • Solidity: uint storedData;

  • Rubidity: uint256 :storedData

    Solidity uses the uint keyword to define a state variable. Rubidity uses a Ruby symbol to define a state variable along with its type, in this case, uint256.

Function Definitions

Set Function

Solidity:

function set(uint x) public {
  storedData = x;
}

Rubidity:

function :set, { x: :uint256 }, :public do
  s.storedData = x;
end

Aside from the use of symbols and the fact that function definition keywords are separated with commas, the implementations are quite similar. The biggest difference is that in Rubidity all function arguments must be named and also that all state variables are prefixed with s.

Get Function

Solidity:

function get() public view returns (uint) {
  return storedData;
}

Rubidity:

function :get, :public, :view, returns: :uint256 do
  return s.storedData
end

Get as well is practically identical.

Constructor

Unlike Solidity, an explicit constructor is required in Rubidity.

Accessing State

In Rubidity, state variables are accessed using the s. prefix, as seen in s.storedData = x and return s.storedData. Writing to state using an unprefixed variable is not possible in Rubidity.

Last updated