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