Struct magnus::class::RClass

source ·
#[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§

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());

Create a new anonymous class.

Examples
use magnus::{class, RClass};

let class = RClass::new(Default::default()).unwrap();
assert!(class.is_kind_of(class::class()));

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(), "");

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());

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>§

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");

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§

The type representing an instance of the class Self.
Create a new anonymous class. Read more
Create a new object, an instance of self, passing the arguments args to the initialiser. Read more
Returns the parent class of self. Read more
Return the name of self. Read more
Return self as an RClass.
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
The resulting type after dereferencing.
Dereferences the value.
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Define a class in self’s scope. Read more
Define a module in self’s scope. Read more
Define an exception class in self’s scope. Read more
Include module into self. Read more
Prepend self with module. Read more
Set the value for the constant name within self’s scope. Read more
Get the value for the constant name within self’s scope. Read more
Returns whether or not self inherits from other. Read more
Return the classes and modules self inherits, includes, or prepends. Read more
Define a method in self’s scope. Read more
Define a private method in self’s scope. Read more
Define a protected method in self’s scope. Read more
Define public accessor methods for the attribute name. Read more
Alias the method src of self as dst. Read more
Define a singleton method in self’s scope. Read more
Get the value for the instance variable name within self’s scope. Read more
Set the value for the instance variable name within self’s scope. Read more
Finds or creates the singleton class of self. Read more
Extend self with module. Read more
Convert val into Self.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.