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§
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
use magnus::{function, prelude::*, rb_assert, Error, Ruby};
fn test() -> i64 {
42
}
fn example(ruby: &Ruby) -> Result<(), Error> {
let module = ruby.define_module("Example")?;
module.define_singleton_method("test", function!(test, 0))?;
rb_assert!(ruby, "Example.test == 42");
Ok(())
}
use magnus::{function, prelude::*, rb_assert, Error, Ruby};
#[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 }
}
}
fn example(ruby: &Ruby) -> Result<(), Error> {
let class = ruby.define_class("Point", ruby.class_object())?;
class.define_singleton_method("new", function!(Point::new, 2))?;
rb_assert!(ruby, "Point.new(1, 2).is_a?(Point)");
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. 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::{prelude::*, Error, RObject, Ruby};
fn example(ruby: &Ruby) -> Result<(), Error> {
let val: RObject = ruby.eval(
r#"
class Example
def initialize(value)
@value = value
end
end
Example.new("foo")
"#,
)?;
assert_eq!(val.ivar_get::<_, String>("@value")?, "foo");
Ok(())
}
Sourcefn ivar_set<T, U>(self, name: T, value: U) -> Result<(), Error>
fn ivar_set<T, U>(self, name: T, value: U) -> Result<(), Error>
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::{prelude::*, rb_assert, Error, RObject, Ruby};
fn example(ruby: &Ruby) -> Result<(), Error> {
let obj: RObject = ruby.eval(
r#"
class Example
def initialize(value)
@value = value
end
def value
@value
end
end
Example.new("foo")
"#,
)?;
obj.ivar_set("@value", "bar")?;
rb_assert!(ruby, r#"obj.value == "bar""#, obj);
Ok(())
}
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::{prelude::*, Error, Ruby};
fn example(ruby: &Ruby) -> Result<(), Error> {
assert!(ruby.str_new("example").singleton_class().is_ok());
Ok(())
}
Sourcefn extend_object(self, module: RModule) -> Result<(), Error>
fn extend_object(self, module: RModule) -> Result<(), Error>
Extend self
with module
.
§Examples
use magnus::{function, prelude::*, rb_assert, Error, RObject, Ruby};
fn test() -> i64 {
42
}
fn example(ruby: &Ruby) -> Result<(), Error> {
let module = ruby.module_new();
module.define_method("test", function!(test, 0))?;
let obj = RObject::try_convert(ruby.class_object().new_instance(())?)?;
obj.extend_object(module)?;
rb_assert!(ruby, "obj.test == 42", obj);
Ok(())
}
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementors§
impl Object for Proc
impl Object for RClass
impl Object for Encoding
impl Object for ExceptionClass
impl Object for Fiber
ruby_gte_3_1
only.