Sha256: ec16e0e7b472a3bdac26025356854f406637f0db4e841e01f612fed778159a20

Contents?: true

Size: 1 KB

Versions: 19

Compression:

Stored size: 1 KB

Contents

use magnus::{function, method, prelude::*, wrap};

#[wrap(class = "Point")]
struct Point {
    x: isize,
    y: isize,
}

impl Point {
    fn new(x: isize, y: isize) -> Self {
        Self { x, y }
    }

    fn x(&self) -> isize {
        self.x
    }

    fn y(&self) -> isize {
        self.y
    }

    fn distance(&self, other: &Point) -> f64 {
        (((other.x - self.x).pow(2) + (other.y - self.y).pow(2)) as f64).sqrt()
    }
}

fn main() -> Result<(), String> {
    magnus::Ruby::init(|ruby| {
        let class = ruby.define_class("Point", ruby.class_object())?;
        class.define_singleton_method("new", function!(Point::new, 2))?;
        class.define_method("x", method!(Point::x, 0))?;
        class.define_method("y", method!(Point::y, 0))?;
        class.define_method("distance", method!(Point::distance, 1))?;

        let d: f64 = ruby.eval(
            "a = Point.new(0, 0)
             b = Point.new(5, 10)
             a.distance(b)",
        )?;

        println!("{}", d);
        Ok(())
    })
}

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/magnus-0.7.1/examples/point.rs
wasmtime-28.0.0 ./ext/cargo-vendor/magnus-0.7.1/examples/point.rs
wasmtime-27.0.0 ./ext/cargo-vendor/magnus-0.7.1/examples/point.rs
wasmtime-26.0.0 ./ext/cargo-vendor/magnus-0.7.1/examples/point.rs
wasmtime-25.0.2 ./ext/cargo-vendor/magnus-0.6.4/examples/point.rs
wasmtime-25.0.1 ./ext/cargo-vendor/magnus-0.6.4/examples/point.rs
wasmtime-25.0.0 ./ext/cargo-vendor/magnus-0.6.4/examples/point.rs
wasmtime-24.0.0 ./ext/cargo-vendor/magnus-0.6.4/examples/point.rs
wasmtime-23.0.2 ./ext/cargo-vendor/magnus-0.6.4/examples/point.rs
wasmtime-22.0.0 ./ext/cargo-vendor/magnus-0.6.4/examples/point.rs
wasmtime-21.0.1 ./ext/cargo-vendor/magnus-0.6.4/examples/point.rs
wasmtime-20.0.2 ./ext/cargo-vendor/magnus-0.6.4/examples/point.rs
wasmtime-20.0.0 ./ext/cargo-vendor/magnus-0.6.4/examples/point.rs
wasmtime-18.0.3 ./ext/cargo-vendor/magnus-0.6.2/examples/point.rs
wasmtime-17.0.1 ./ext/cargo-vendor/magnus-0.6.2/examples/point.rs
wasmtime-17.0.0 ./ext/cargo-vendor/magnus-0.6.2/examples/point.rs
wasmtime-16.0.0 ./ext/cargo-vendor/magnus-0.6.2/examples/point.rs
wasmtime-15.0.1 ./ext/cargo-vendor/magnus-0.6.2/examples/point.rs
wasmtime-15.0.0 ./ext/cargo-vendor/magnus-0.6.2/examples/point.rs