#[repr(transparent)]pub struct RClass(_);
Expand description
A Value pointer to a RClass struct, Ruby’s internal representation of classes.
See the Module
trait for defining instance methods and nested
classes/modules.
See the Object
trait for defining singlton methods (aka class methods).
All Value
methods should be available on this type through Deref
,
but some may be missed by this documentation.
Implementations§
source§impl RClass
impl RClass
sourcepub fn from_value(val: Value) -> Option<Self>
pub fn from_value(val: Value) -> Option<Self>
Return Some(RClass)
if val
is a RClass
, None
otherwise.
Examples
use magnus::{eval, RClass};
assert!(RClass::from_value(eval("String").unwrap()).is_some());
assert!(RClass::from_value(eval("Enumerable").unwrap()).is_none());
assert!(RClass::from_value(eval("nil").unwrap()).is_none());
sourcepub fn new(superclass: RClass) -> Result<RClass, Error>
pub fn new(superclass: RClass) -> Result<RClass, Error>
Create a new anonymous class.
Examples
use magnus::{class, RClass};
let class = RClass::new(Default::default()).unwrap();
assert!(class.is_kind_of(class::class()));
sourcepub fn new_instance<T>(self, args: T) -> Result<Value, Error>where
T: ArgList,
pub fn new_instance<T>(self, args: T) -> Result<Value, Error>where
T: ArgList,
Create a new object, an instance of self
, passing the arguments
args
to the initialiser.
Examples
use magnus::class;
let s = class::string().new_instance(()).unwrap();
assert!(s.is_kind_of(class::string()));
assert_eq!(s.to_string(), "");
sourcepub fn superclass(self) -> Result<Self, Error>
pub fn superclass(self) -> Result<Self, Error>
Returns the parent class of self
.
Returns Err
if self
can not have a parent class.
Examples
use magnus::{class, eval};
let klass = class::hash().superclass().unwrap();
assert!(klass.equal(class::object()).unwrap());
sourcepub unsafe fn name(&self) -> Cow<'_, str>
pub 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::{class, eval};
let value = class::hash();
// safe as we neve give Ruby a chance to free the string.
let s = unsafe { value.name() }.into_owned();
assert_eq!(s, "Hash");
Methods from Deref<Target = Value>§
sourcepub unsafe fn to_s(&self) -> Result<Cow<'_, str>, Error>
pub unsafe fn to_s(&self) -> Result<Cow<'_, str>, Error>
Convert self
to a Rust string.
Safety
This may return a direct view of memory owned and managed by Ruby. 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::{eval, QTRUE};
let value = QTRUE;
// safe as we neve give Ruby a chance to free the string.
let s = unsafe { value.to_s() }.unwrap().into_owned();
assert_eq!(s, "true");
sourcepub unsafe fn classname(&self) -> Cow<'_, str>
pub unsafe fn classname(&self) -> Cow<'_, str>
Return the name of self
’s class.
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::{eval, RHash};
let value = RHash::new();
// safe as we never give Ruby a chance to free the string.
let s = unsafe { value.classname() }.into_owned();
assert_eq!(s, "Hash");
Trait Implementations§
source§impl Class for RClass
impl Class for RClass
source§impl Module for RClass
impl Module for RClass
source§fn define_class<T: Into<Id>>(
self,
name: T,
superclass: RClass
) -> Result<RClass, Error>
fn define_class<T: Into<Id>>(
self,
name: T,
superclass: RClass
) -> Result<RClass, Error>
self
’s scope. Read moresource§fn define_module<T: Into<Id>>(self, name: T) -> Result<RModule, Error>
fn define_module<T: Into<Id>>(self, name: T) -> Result<RModule, Error>
self
’s scope. Read moresource§fn define_error<T: Into<Id>>(
self,
name: T,
superclass: ExceptionClass
) -> Result<ExceptionClass, Error>
fn define_error<T: Into<Id>>(
self,
name: T,
superclass: ExceptionClass
) -> Result<ExceptionClass, Error>
self
’s scope. Read moresource§fn const_set<T, U>(self, name: T, value: U) -> Result<(), Error>where
T: Into<Id>,
U: Into<Value>,
fn const_set<T, U>(self, name: T, value: U) -> Result<(), Error>where
T: Into<Id>,
U: Into<Value>,
source§fn ancestors(self) -> RArray
fn ancestors(self) -> RArray
self
inherits, includes, or prepends. Read moresource§fn define_method<T, M>(self, name: T, func: M) -> Result<(), Error>where
T: Into<Id>,
M: Method,
fn define_method<T, M>(self, name: T, func: M) -> Result<(), Error>where
T: Into<Id>,
M: Method,
self
’s scope. Read moresource§fn define_private_method<M>(self, name: &str, func: M) -> Result<(), Error>where
M: Method,
fn define_private_method<M>(self, name: &str, func: M) -> Result<(), Error>where
M: Method,
self
’s scope. Read moresource§fn define_protected_method<M>(self, name: &str, func: M) -> Result<(), Error>where
M: Method,
fn define_protected_method<M>(self, name: &str, func: M) -> Result<(), Error>where
M: Method,
self
’s scope. Read moresource§impl Object for RClass
impl Object for RClass
source§fn 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,
self
’s scope. Read more