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§
Required Methods§
sourcefn new(superclass: Self) -> Result<Self, Error>
fn new(superclass: Self) -> Result<Self, Error>
Create a new anonymous class.
Examples
use magnus::{exception, prelude::*, ExceptionClass};
assert!(ExceptionClass::new(exception::standard_error()).is_ok());
sourcefn new_instance<T>(self, args: T) -> Result<Self::Instance, Error>where
T: ArgList,
fn new_instance<T>(self, args: T) -> Result<Self::Instance, Error>where
T: ArgList,
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§
sourcefn superclass(self) -> Result<RClass, Error>
fn superclass(self) -> Result<RClass, Error>
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());
sourceunsafe fn name(&self) -> Cow<'_, str>
unsafe fn name(&self) -> Cow<'_, str>
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");
sourcefn as_r_class(self) -> RClass
fn as_r_class(self) -> RClass
Return self
as an RClass
.