Trait magnus::prelude::Object

source ·
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§

Define a singleton method in self’s scope.

Singleton methods defined on a class are Ruby’s method for implementing ‘class’ methods.

Get the value for the instance variable name within self’s scope.

Note, the @ is part of the name.

Set the value for the instance variable name within self’s scope.

Note, the @ is part of the name.

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());

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);

Implementors§