Project

rinru

0.0
The project is in a healthy, maintained state
Rinru - Portable self-contained Rust engine.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Runtime

~> 1.15
 Project Readme

Rinru

Rinru (Rust-in-Ruby) is a framework distributed as a self-contained Ruby gem. It allows you to write and compile complete Rust code ecosystems natively and inject them directly into your running Ruby scripts as global methods.

Features

  • Fluent Injection Pipeline: Link a Cargo configuration and .rs source file straight to a global method name out of thin air.
  • Embedded Toolchain: Carries its own private copy of the Rust compiler binary. Users do not need rustup or system-wide Rust paths configured to use it.
  • Dynamic Variable Ingestion: Accepts numbers, floating points, and text strings from Ruby, coordinating all FFI pointer allocations automatically.
  • Incremental MD5 Caching: Skips compiling on subsequent runs if your Rust files have not changed, keeping execution overhead at zero.
  • Graceful Error Catching: Intercepts Rust syntax compilation errors (stderr) and formats them into clean Ruby exception logs rather than crashing your terminal runtime.

Installation

You can install the self-contained framework directly onto your system using:

gem install rinru

Or add this line to your application's Gemfile:

gem 'rinru'

And then execute:

bundle install

Usage

1. Setup a standard Cargo Project Layout

Create a project manifest folder (project/Cargo.toml):

[package]
name = "rinru_math_engine"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]
evalexpr = "8.1"

Write your high-performance code inside your target source file (project/src/main.rs):

fn main(expression_ptr: *const std::os::raw::c_char) -> i32 {
    if expression_ptr.is_null() { return 0; }
    
    let c_str = unsafe { std::ffi::CStr::from_ptr(expression_ptr) };
    let expr_str = c_str.to_str().unwrap_or("0");

    // Let the evalexpr crate parse and compute the string operations natively
    match evalexpr::eval_int(expr_str) {
        Ok(result) => result as i32,
        Err(_) => 0,
    }
}

2. Run it in your Ruby Script

Simply chain your configurations together. Once declared, you invoke the variable name as a top-level native Ruby method:

require 'rinru'

# Chain cargo parsing straight into your target method injection
rinru.cargo("project/Cargo.toml").var("project/src/main.rs").as("main")

# Pass raw math string expressions. Rust calculates the order of operations natively!
puts main("5 * (10 + 20) - 5")
# => 145

How It Works Under the Hood

  1. rinru.cargo().var() maps out the locations of your source script files.
  2. .as("main") invokes Rinru's internal rustc environment compiler. It reviews an MD5 hash of your code state. If unchanged, it pulls the library straight from cache; otherwise, it silently generates an optimized release binary .so/.dylib.
  3. Dynamic FFI Proxying: It automatically binds the binary function directly into Ruby's root Kernel workspace, letting you invoke the method natively anywhere in your application.

License

The gem is available as open source under the terms of the MIT License.