Rubidity by Example
Last updated
Last updated
Let's start with some examples from .
This basic Solidity contract sets the value of a variable and then exposes it to others to access:
Here is how this logic translates to Rubidity:
Looking point by point, here's what the Solidity version does and how Rubidity translates it.
Solidity: contract SimpleStorage { ... }
Rubidity: class Contracts::SimpleStorage < Contract
In Solidity, you define a contract using the contract
keyword. In Rubidity, you define a contract as a Ruby class that inherits from a base class called Contract
.
State Variable
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
.
Solidity:
Rubidity:
The set
function is public in both languages and takes an unsigned integer as an argument. In Rubidity, the function signature also specifies its visibility (:public
) and arguments ({ x: :uint256 }
).
Solidity:
Rubidity:
The get
function is public and has a view
property in both languages. The Solidity version uses the returns
keyword to specify the return type, while Rubidity does it via the returns: :uint256
option.
Constructor
Solidity: No explicit constructor.
Rubidity: constructor() {}
Both Solidity and Rubidity examples don't utilize a constructor for any initial setup, but the Rubidity code explicitly includes an empty constructor for clarity.
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 Ruby and the explicitness of s.
is nice anyway.
Let's break down this OpenMintToken
Rubidity contract line-by-line, focusing on how it might translate to a Solidity contract. The contract is an ERC20 token with a capped supply and additional limitations on individual mints.
Inheritance: is :ERC20
This line means that the OpenMintToken
contract inherits from an existing ERC20 contract, inheriting its state variables, functions, and logic.
State Variables: uint256 :public, :maxSupply
and uint256 :public, :perMintLimit
These lines define two public state variables, maxSupply
and perMintLimit
. Public state variables are accessible to external contracts and can also have getter methods generated automatically.
Constructor:
The constructor function initializes the contract. It takes the token's name, symbol, maximum supply, per-mint limit, and decimals as parameters.
State Variable Initialization: s.maxSupply = maxSupply
and s.perMintLimit = perMintLimit
These lines initialize the state variables using the s.
prefix, which is specific to Rubidity for accessing and manipulating state variables.
Mint Function:
This is a public function to mint new tokens. The require
statements serve as checks that the amount
is positive, within the per-mint limit, and won't exceed the max supply. _mint
is a likely internal function inherited from the ERC20 contract that actually mints the tokens.
Airdrop Function:
Similar to the mint
function but allows specifying a recipient address to
. It performs the same require
checks and then invokes _mint
to create the tokens for the target address.
The Rubidity code uses specific language constructs that mirror Solidity but in a Ruby-like syntax, making it more expressive while maintaining similar logic and functionalities.