pub trait Module: Object + Deref<Target = Value> + Copy {
fn define_class<T: Into<Id>>(
self,
name: T,
superclass: RClass
) -> Result<RClass, Error> { ... }
fn define_module<T: Into<Id>>(self, name: T) -> Result<RModule, Error> { ... }
fn const_get<T, U>(self, name: T) -> Result<U, Error>
where
T: Into<Id>,
U: TryConvert,
{ ... }
fn is_inherited<T>(self, other: T) -> bool
where
T: Deref<Target = Value> + Module,
{ ... }
fn define_method<T, M>(self, name: T, func: M)
where
T: Into<Id>,
M: Method,
{ ... }
fn define_private_method<M>(self, name: &str, func: M)
where
M: Method,
{ ... }
fn define_protected_method<M>(self, name: &str, func: M)
where
M: Method,
{ ... }
}
Expand description
Functions available on both classes and modules.
Provided methods
Define a class in self
’s scope.
Examples
use magnus::{class, define_module, Module};
let outer = define_module("Outer").unwrap();
let inner = outer.define_class("Inner", Default::default()).unwrap();
assert!(inner.is_kind_of(class::class()));
Define a module in self
’s scope.
Examples
use magnus::{class, define_module, Module};
let outer = define_module("Outer").unwrap();
let inner = outer.define_module("Inner").unwrap();
assert!(inner.is_kind_of(class::module()));
Get the value for the constant name
within self
’s scope.
Examples
use magnus::{class, eval, Module, RClass, Value};
eval::<Value>("
class Example
VALUE = 42
end
").unwrap();
let class = class::object().const_get::<_, RClass>("Example").unwrap();
assert_eq!(class.const_get::<_, i64>("VALUE").unwrap(), 42);
Returns whether or not self
inherits from other
.
Classes including a module are considered to inherit from that module.
Examples
use magnus::{eval, Module, RClass};
let a = RClass::new(Default::default()).unwrap();
let b = RClass::new(a).unwrap();
assert!(b.is_inherited(a));
assert!(!a.is_inherited(b));
fn define_method<T, M>(self, name: T, func: M) where
T: Into<Id>,
M: Method,
fn define_method<T, M>(self, name: T, func: M) where
T: Into<Id>,
M: Method,
Define a method in self
’s scope.
Examples
use magnus::{class, eval, method, Module};
fn escape_unicode(s: String) -> String {
s.escape_unicode().to_string()
}
class::string().define_method("escape_unicode", method!(escape_unicode, 0));
let res = eval::<bool>(r#""🤖\etest".escape_unicode == "\\u{1f916}\\u{1b}\\u{74}\\u{65}\\u{73}\\u{74}""#).unwrap();
assert!(res);
fn define_private_method<M>(self, name: &str, func: M) where
M: Method,
fn define_private_method<M>(self, name: &str, func: M) where
M: Method,
Define a private method in self
’s scope.
Examples
use magnus::{class, eval, exception, function, Module, Value};
fn percent_encode(c: char) -> String {
if c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '.' || c == '~' {
String::from(c)
} else {
format!("%{:X}", c as u32)
}
}
class::string().define_private_method("percent_encode_char", function!(percent_encode, 1));
eval::<Value>(r#"
class String
def percent_encode
chars.map {|c| percent_encode_char(c)}.join("")
end
end
"#).unwrap();
let res = eval::<bool>(r#""foo bar".percent_encode == "foo%20bar""#).unwrap();
assert!(res);
assert!(eval::<bool>(r#"" ".percent_encode_char(" ")"#).unwrap_err().is_kind_of(exception::no_method_error()));
fn define_protected_method<M>(self, name: &str, func: M) where
M: Method,
fn define_protected_method<M>(self, name: &str, func: M) where
M: Method,
Define a protected method in self
’s scope.
Examples
use magnus::{class, eval, exception, method, Module, Value};
fn escape_unicode(s: String) -> String {
s.escape_unicode().to_string()
}
fn is_invisible(c: char) -> bool {
c.is_control() || c.is_whitespace()
}
class::string().define_method("escape_unicode", method!(escape_unicode, 0));
class::string().define_protected_method("invisible?", method!(is_invisible, 0));
eval::<Value>(r#"
class String
def escape_invisible
chars.map {|c| c.invisible? ? c.escape_unicode : c}.join("")
end
end
"#).unwrap();
let res: bool = eval!(r#"
"🤖\tfoo bar".escape_invisible == "🤖\\u{9}foo\\u{20}bar"
"#).unwrap();
assert!(res);
assert!(eval::<bool>(r#"" ".invisible?"#).unwrap_err().is_kind_of(exception::no_method_error()));