pub trait Object: Deref<Target = Value> + ReprValue + Copy {
// Provided methods
fn define_singleton_method<M>(self, name: &str, func: M) -> Result<(), Error>
where M: Method { ... }
fn ivar_get<T, U>(self, name: T) -> Result<U, Error>
where T: IntoId,
U: TryConvert { ... }
fn ivar_set<T, U>(self, name: T, value: U) -> Result<(), Error>
where T: IntoId,
U: IntoValue { ... }
fn singleton_class(self) -> Result<RClass, Error> { ... }
fn extend_object(self, module: RModule) -> Result<(), Error> { ... }
}
Expand description
Functions available all non-immediate values.
Provided Methods§
sourcefn define_singleton_method<M>(self, name: &str, func: M) -> Result<(), Error>where
M: Method,
fn define_singleton_method<M>(self, name: &str, func: M) -> Result<(), Error>where M: Method,
Define a singleton method in self
’s scope.
Singleton methods defined on a class are Ruby’s method for implementing ‘class’ methods.
Examples found in repository?
examples/point.rs (line 31)
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
fn main() -> Result<(), Error> {
let _cleanup = unsafe { embed::init() };
let class = define_class("Point", 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 = eval(
"a = Point.new(0, 0)
b = Point.new(5, 10)
a.distance(b)",
)?;
println!("{}", d);
Ok(())
}
More examples
examples/mut_point.rs (line 43)
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
fn main() -> Result<(), Error> {
let _cleanup = unsafe { embed::init() };
let class = define_class("Point", class::object())?;
class.define_singleton_method("new", function!(MutPoint::new, 2))?;
class.define_method("x", method!(MutPoint::x, 0))?;
class.define_method("x=", method!(MutPoint::set_x, 1))?;
class.define_method("y", method!(MutPoint::y, 0))?;
class.define_method("y=", method!(MutPoint::set_y, 1))?;
class.define_method("distance", method!(MutPoint::distance, 1))?;
let d: f64 = eval(
"a = Point.new(0, 0)
b = Point.new(0, 0)
b.x = 5
b.y = 10
a.distance(b)",
)?;
println!("{}", d);
Ok(())
}
sourcefn ivar_get<T, U>(self, name: T) -> Result<U, Error>where
T: IntoId,
U: TryConvert,
fn ivar_get<T, U>(self, name: T) -> Result<U, Error>where T: IntoId, U: TryConvert,
Get the value for the instance variable name
within self
’s scope.
Note, the @
is part of the name.
sourcefn ivar_set<T, U>(self, name: T, value: U) -> Result<(), Error>where
T: IntoId,
U: IntoValue,
fn ivar_set<T, U>(self, name: T, value: U) -> Result<(), Error>where T: IntoId, U: IntoValue,
Set the value for the instance variable name
within self
’s scope.
Note, the @
is part of the name.
sourcefn singleton_class(self) -> Result<RClass, Error>
fn singleton_class(self) -> Result<RClass, Error>
Finds or creates the singleton class of self
.
Returns Err
if self
can not have a singleton class.
Examples
use magnus::{Object, RString};
assert!(RString::new("example").singleton_class().is_ok());
sourcefn extend_object(self, module: RModule) -> Result<(), Error>
fn extend_object(self, module: RModule) -> Result<(), Error>
Extend self
with module
.
Examples
use magnus::{class, function, Module, Object, RModule, RObject};
fn example() -> i64 {
42
}
let module = RModule::new();
module.define_method("example", function!(example, 0)).unwrap();
let obj: RObject = class::object().new_instance(()).unwrap().try_convert().unwrap();
obj.extend_object(module).unwrap();
assert_eq!(obj.funcall::<_, _, i64>("example", ()).unwrap(), 42);