pub trait Object: Deref<Target = Value> + Copy {
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: Into<Id>,
U: TryConvert,
{ ... }
fn ivar_set<T, U>(self, name: T, value: U) -> Result<(), Error>
where
T: Into<Id>,
U: Into<Value>,
{ ... }
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.
sourcefn ivar_get<T, U>(self, name: T) -> Result<U, Error>where
T: Into<Id>,
U: TryConvert,
fn ivar_get<T, U>(self, name: T) -> Result<U, Error>where
T: Into<Id>,
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: Into<Id>,
U: Into<Value>,
fn ivar_set<T, U>(self, name: T, value: U) -> Result<(), Error>where
T: Into<Id>,
U: Into<Value>,
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);