State variables are data elements that store the contract's state. In Rubidity, you can define state variables, specify their types, and set visibility options (public, private, or internal) along with additional flags like immutable and constant.
The basic syntax to define a state variable is as follows:
type :visibility, :variable_name, :flagstype: The type of the variable (uint256,string, etc.)visibility: Visibility of the variable (public,private, orinternal)variable_name: The name of the state variableflags: Additional flags like:immutableor:constant
Declare a public string variable named name:
string :public, :nameDeclare a public unsigned integer named totalSupply:
uint256 :public, :totalSupplyFor complex types like mappings and arrays, special syntax is used:
mapping ({ key_type: :value_type }), :visibility, :variable_namearray :value_type, :visibility, :variable_nameMuch like Solidity, for every public state variable, Rubidity automatically generates a getter function.
In your contract logic, you can access state variables using the s object:
s.variable_nameExample:
# Accessing a state variable
function :get_balance, { addr: :address }, :public, :view, returns: :uint256 do
return s.balances[addr] # Accessing s.balances
end:immutable: The variable can only be set once, typically in the constructor.:constant: The variable's value is set at compile time and cannot be changed.
\