Packages
metastatic
0.20.3
0.26.0
0.25.0
0.24.1
0.24.0
0.23.0
0.22.2
0.22.1
0.22.0
0.21.3
0.21.2
0.21.1
0.21.0
0.20.3
0.20.2
0.20.1
0.20.0
0.19.0
0.18.0
0.17.0
0.16.0
0.15.1
0.15.0
0.14.2
0.14.1
0.14.0
0.13.3
0.13.2
0.13.1
0.13.0
0.12.0
0.11.0
0.10.4
0.10.3
0.10.2
0.10.1
0.10.0
0.9.2
0.9.1
0.9.0
0.8.6
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.7
0.7.6
0.7.5
0.7.4
0.7.3
0.7.1
0.7.0
0.6.1
0.6.0
0.5.2
0.5.1
0.5.0
0.4.2
0.4.1
0.4.0
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.2.0
0.1.3
0.1.2
0.1.1
0.1.0
Cross-language code meta-model library using unified MetaAST representation. Parse, transform, and translate code across Python, Elixir, Ruby, Erlang, Haskell, and more via a shared three-tuple AST format.
Current section
Files
Jump to
Current section
Files
lib/metastatic/adapters/ruby.ex
defmodule Metastatic.Adapters.Ruby do
@moduledoc """
Ruby language adapter for MetaAST transformations.
Bridges between Ruby AST (M1) and MetaAST (M2), enabling cross-language
code analysis and transformation for Ruby source code.
## Ruby AST Structure (M1)
Ruby uses the parser gem which represents AST as nodes with:
- `type` - Symbol representing the syntactic construct
- `children` - List of child nodes or values
- `location` - Source location information
### Examples
# Variable assignment
%{type: "lvasgn", children: ["x", %{type: "int", children: [42]}]}
# Method call
%{type: "send", children: [receiver, :method_name, args...]}
# Binary operation
%{type: "send", children: [left, :+, right]}
## M1 ↔ M2 Transformations
This adapter performs bidirectional transformations between Ruby AST (M1)
and MetaAST (M2):
### Literals
# M1 → M2
%{type: "int", children: [42]} → {:literal, :integer, 42}
%{type: "float", children: [3.14]} → {:literal, :float, 3.14}
%{type: "str", children: ["hello"]} → {:literal, :string, "hello"}
%{type: "true"} → {:literal, :boolean, true}
%{type: "nil"} → {:literal, :null, nil}
%{type: "sym", children: [:foo]} → {:literal, :symbol, :foo}
### Variables
%{type: "lvar", children: [:x]} → {:variable, "x"}
%{type: "ivar", children: [:@x]} → {:variable, "@x"}
### Binary Operations
# x + 5
%{type: "send", children: [x_node, :+, five_node]} → {:binary_op, :arithmetic, :+, left, right}
## Round-Trip Fidelity
The adapter achieves >95% round-trip fidelity for M2.1 (Core) constructs.
Metadata preserves information like:
- Line numbers and columns
- Variable scopes (local, instance, class, global)
- Original syntax variants
## Usage
# Parse Ruby source
{:ok, ast} = Metastatic.Adapters.Ruby.parse("x = 42")
# Transform to MetaAST
{:ok, meta_ast, metadata} = Metastatic.Adapters.Ruby.to_meta(ast)
# Transform back to Ruby AST
{:ok, ast2} = Metastatic.Adapters.Ruby.from_meta(meta_ast, metadata)
# Unparse to source
{:ok, source} = Metastatic.Adapters.Ruby.unparse(ast2)
"""
@behaviour Metastatic.Adapter
alias Metastatic.Adapters.Ruby.{FromMeta, Subprocess, ToMeta}
alias Metastatic.Semantic.Enricher
@impl true
def parse(source) when is_binary(source) do
Subprocess.parse(source)
end
@impl true
def to_meta(ruby_ast) do
case ToMeta.transform(ruby_ast) do
{:ok, meta_ast, metadata} ->
# Enrich AST with semantic metadata (op_kind for DB operations, etc.)
enriched_ast = Enricher.enrich_tree(meta_ast, :ruby)
{:ok, enriched_ast, metadata}
error ->
error
end
end
@impl true
def from_meta(meta_ast, metadata) do
FromMeta.transform(meta_ast, metadata)
end
@impl true
def unparse(ruby_ast) do
Subprocess.unparse(ruby_ast)
end
@impl true
def file_extensions do
[".rb"]
end
end