#[repr(transparent)]pub struct RModule(_);
Expand description
A Value pointer to a RModule struct, Ruby’s internal representation of modules.
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
sourceimpl RModule
impl RModule
sourcepub fn from_value(val: Value) -> Option<Self>
pub fn from_value(val: Value) -> Option<Self>
Return Some(RModule)
if val
is a RModule
, None
otherwise.
Examples
use magnus::{eval, RModule};
assert!(RModule::from_value(eval("Enumerable").unwrap()).is_some());
assert!(RModule::from_value(eval("String").unwrap()).is_none());
assert!(RModule::from_value(eval("nil").unwrap()).is_none());
sourcepub fn new() -> Self
pub fn new() -> Self
Create a new anonymous module.
Examples
use magnus::{class, RModule};
let module = RModule::new();
assert!(module.is_kind_of(class::module()));
sourcepub fn define_module_function<M>(self, name: &str, func: M) -> Result<(), Error> where
M: Method,
pub fn define_module_function<M>(self, name: &str, func: M) -> Result<(), Error> where
M: Method,
Define a method in self
’s scope as a ‘module function’. This method
will be visible as a public ‘class’ method on the module and a private
instance method on any object including the module.
Examples
use magnus::{define_module, eval, function, r_string, RString};
fn greet() -> RString {
r_string!("Hello, world!")
}
let module = define_module("Greeting").unwrap();
module.define_module_function("greet", function!(greet, 0)).unwrap();
let res = eval::<bool>(r#"Greeting.greet == "Hello, world!""#).unwrap();
assert!(res);
let res = eval::<bool>(r#"
include Greeting
greet == "Hello, world!"
"#).unwrap();
assert!(res);
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 neve give Ruby a chance to free the string.
let s = unsafe { value.classname() }.into_owned();
assert_eq!(s, "Hash");
sourcepub fn try_convert<T>(&self) -> Result<T, Error> where
T: TryConvert,
pub fn try_convert<T>(&self) -> Result<T, Error> where
T: TryConvert,
Convert self
to the Rust type T
.
See the types that TryConvert
is implemented on for what this
method can convert to.
Examples
use magnus::{eval, Value};
assert_eq!(eval::<Value>("42").unwrap().try_convert::<i64>().unwrap(), 42);
assert_eq!(eval::<Value>("1.23").unwrap().try_convert::<i64>().unwrap(), 1);
assert_eq!(eval::<Value>("1").unwrap().try_convert::<f64>().unwrap(), 1.0);
assert_eq!(eval::<Value>("nil").unwrap().try_convert::<Option<i64>>().unwrap(), None);
assert_eq!(eval::<Value>("42").unwrap().try_convert::<Option<i64>>().unwrap(), Some(42));
Trait Implementations
sourceimpl Module for RModule
impl Module for RModule
sourcefn 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>
Define a class in self
’s scope. Read more
sourcefn define_module<T: Into<Id>>(self, name: T) -> Result<RModule, Error>
fn define_module<T: Into<Id>>(self, name: T) -> Result<RModule, Error>
Define a module in self
’s scope. Read more
sourcefn include_module(self, module: RModule) -> Result<(), Error>
fn include_module(self, module: RModule) -> Result<(), Error>
Include module
into self
. Read more
sourcefn prepend_module(self, module: RModule) -> Result<(), Error>
fn prepend_module(self, module: RModule) -> Result<(), Error>
Prepend self
with module
. Read more
sourcefn 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>,
Set the value for the constant name
within self
’s scope. Read more
sourcefn const_get<T, U>(self, name: T) -> Result<U, Error> where
T: Into<Id>,
U: TryConvert,
fn const_get<T, U>(self, name: T) -> Result<U, Error> where
T: Into<Id>,
U: TryConvert,
Get the value for the constant name
within self
’s scope. Read more
sourcefn is_inherited<T>(self, other: T) -> bool where
T: Deref<Target = Value> + Module,
fn is_inherited<T>(self, other: T) -> bool where
T: Deref<Target = Value> + Module,
Returns whether or not self
inherits from other
. Read more
sourcefn ancestors(self) -> RArray
fn ancestors(self) -> RArray
Return the classes and modules self
inherits, includes, or prepends. Read more
sourcefn 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,
Define a method in self
’s scope. Read more
sourcefn 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,
Define a private method in self
’s scope. Read more
sourcefn 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,
Define a protected method in self
’s scope. Read more
sourceimpl Object for RModule
impl Object for RModule
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. Read more
sourcefn ivar_get<T, U>(self, name: T) -> Result<U, Error> where
T: Into<Id>,
U: TryConvert,
fn ivar_get<T, U>(self, name: T) -> Result<U, Error> where
T: Into<Id>,
U: TryConvert,
Get the value for the instance variable name
within self
’s scope. Read more
sourcefn ivar_set<T, U>(self, name: T, value: U) -> Result<(), Error> where
T: Into<Id>,
U: Into<Value>,
fn ivar_set<T, U>(self, name: T, value: U) -> Result<(), Error> where
T: Into<Id>,
U: Into<Value>,
Set the value for the instance variable name
within self
’s scope. Read more
sourceimpl TryConvert for RModule
impl TryConvert for RModule
sourcefn try_convert(val: &Value) -> Result<Self, Error>
fn try_convert(val: &Value) -> Result<Self, Error>
Convert val
into Self
.
impl Copy for RModule
impl ReprValue for RModule
Auto Trait Implementations
impl RefUnwindSafe for RModule
impl Send for RModule
impl Sync for RModule
impl Unpin for RModule
impl UnwindSafe for RModule
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more