Trait magnus::class::Class

source ·
pub trait Class: Module {
    type Instance;

    fn new(superclass: Self) -> Result<Self, Error>;
    fn new_instance<T>(self, args: T) -> Result<Self::Instance, Error>
    where
        T: ArgList
; fn superclass(self) -> Result<RClass, Error> { ... } unsafe fn name(&self) -> Cow<'_, str> { ... } fn as_r_class(self) -> RClass { ... } }
Expand description

Functions available on all types representing a Ruby class.

Required Associated Types§

The type representing an instance of the class Self.

Required Methods§

Create a new anonymous class.

Examples
use magnus::{exception, prelude::*, ExceptionClass};

assert!(ExceptionClass::new(exception::standard_error()).is_ok());

Create a new object, an instance of self, passing the arguments args to the initialiser.

Examples
use magnus::{exception, prelude::*};

let s = exception::standard_error().new_instance(("bang!",)).unwrap();
assert!(s.is_kind_of(exception::standard_error()));

Provided Methods§

Returns the parent class of self.

Returns Err if self can not have a parent class.

Examples
use magnus::{class, exception, prelude::*};

let klass = exception::exception().superclass().unwrap();
assert!(klass.equal(class::object()).unwrap());

Return the name of self.

Safety

Ruby may modify or free the memory backing the returned str, the caller must ensure this does not happen.

This can be used safely by immediately calling into_owned on the return value.

Examples
use magnus::{exception, prelude::*};

let value = exception::standard_error();
// safe as we neve give Ruby a chance to free the string.
let s = unsafe { value.name() }.into_owned();
assert_eq!(s, "StandardError");

Return self as an RClass.

Implementors§