Trait magnus::Object

source ·
pub trait Object: 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§

source

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
use magnus::{define_module, function, prelude::*, rb_assert};

fn example() -> i64 {
    42
}

let module = define_module("Example").unwrap();
module
    .define_singleton_method("example", function!(example, 0))
    .unwrap();
rb_assert!("Example.example == 42");
use magnus::{class, define_class, function, prelude::*, rb_assert};

#[magnus::wrap(class = "Point", free_immediately, size)]
struct Point {
    x: isize,
    y: isize,
}

impl Point {
    fn new(x: isize, y: isize) -> Self {
        Self { x, y }
    }
}

let class = define_class("Point", class::object()).unwrap();
class
    .define_singleton_method("new", function!(Point::new, 2))
    .unwrap();

rb_assert!("Point.new(1, 2).is_a?(Point)");
source

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. An instance variable can be set and retrieved without a preceding @ and it will work, but the instance variable will be invisible to Ruby code.

§Examples
use magnus::{eval, prelude::*, RObject};

let val: RObject = eval(
    r#"
      class Example
        def initialize(value)
          @value = value
        end
      end
      Example.new("foo")
    "#,
)
.unwrap();

assert_eq!(val.ivar_get::<_, String>("@value").unwrap(), "foo");
source

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. Setting an instance variable without a preceding @ will work, but the instance variable will be invisible to Ruby code.

§Examples
use magnus::{eval, prelude::*, rb_assert, RObject};

let obj: RObject = eval(
    r#"
      class Example
        def initialize(value)
          @value = value
        end

        def value
          @value
        end
      end
      Example.new("foo")
    "#,
)
.unwrap();

obj.ivar_set("@value", "bar").unwrap();
rb_assert!(r#"obj.value == "bar""#, obj);
source

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::{prelude::*, RString};

assert!(RString::new("example").singleton_class().is_ok());
source

fn extend_object(self, module: RModule) -> Result<(), Error>

Extend self with module.

§Examples
use magnus::{class, function, prelude::*, rb_assert, RModule, RObject};

fn example() -> i64 {
    42
}

let module = RModule::new();
module
    .define_method("example", function!(example, 0))
    .unwrap();

let obj = RObject::try_convert(class::object().new_instance(()).unwrap()).unwrap();
obj.extend_object(module).unwrap();
rb_assert!("obj.example == 42", obj);

Object Safety§

This trait is not object safe.

Implementors§