matsadler / magnus

Ruby bindings for Rust. Write Ruby extension gems in Rust, or call Ruby from Rust.
https://docs.rs/magnus/latest/magnus/
MIT License
682 stars 35 forks source link

Exposing mutable wrapped class methods #98

Closed Zapotek closed 10 months ago

Zapotek commented 11 months ago

Hello,

In the Point example, how would one continue to expose the set_x method to Ruby?

Cheers!

matsadler commented 11 months ago

There's a full example in the examples/ directory, it'd look like:

class.define_method("x=", method!(MutPoint::set_x, 1))?;

then from Ruby you'd use it like:

point = Point.new(1, 2)
point.x    #=> 1
point.x = 3
point.x    #=> 3