pub trait Module: Object + ReprValue + Copy {
Show 14 methods
// Provided methods
fn define_class<T>(
self,
name: T,
superclass: RClass
) -> Result<RClass, Error>
where T: IntoId { ... }
fn define_module<T>(self, name: T) -> Result<RModule, Error>
where T: IntoId { ... }
fn define_error<T>(
self,
name: T,
superclass: ExceptionClass
) -> Result<ExceptionClass, Error>
where T: IntoId { ... }
fn include_module(self, module: RModule) -> Result<(), Error> { ... }
fn prepend_module(self, module: RModule) -> Result<(), Error> { ... }
fn const_set<T, U>(self, name: T, value: U) -> Result<(), Error>
where T: IntoId,
U: IntoValue { ... }
fn const_get<T, U>(self, name: T) -> Result<U, Error>
where T: IntoId,
U: TryConvert { ... }
fn is_inherited<T>(self, other: T) -> bool
where T: ReprValue + Module { ... }
fn ancestors(self) -> RArray { ... }
fn define_method<T, M>(self, name: T, func: M) -> Result<(), Error>
where T: IntoId,
M: Method { ... }
fn define_private_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 { ... }
fn define_attr<T>(self, name: T, rw: Attr) -> Result<(), Error>
where T: IntoId { ... }
fn define_alias<T, U>(self, dst: T, src: U) -> Result<(), Error>
where T: IntoId,
U: IntoId { ... }
}
Expand description
Functions available on both classes and modules.
Provided Methods§
sourcefn define_class<T>(self, name: T, superclass: RClass) -> Result<RClass, Error>where
T: IntoId,
fn define_class<T>(self, name: T, superclass: RClass) -> Result<RClass, Error>where
T: IntoId,
Define a class in self
’s scope.
§Examples
use magnus::{class, define_module, prelude::*, rb_assert};
let outer = define_module("Outer").unwrap();
outer.define_class("Inner", class::object()).unwrap();
rb_assert!("Outer::Inner.is_a?(Class)");
sourcefn define_module<T>(self, name: T) -> Result<RModule, Error>where
T: IntoId,
fn define_module<T>(self, name: T) -> Result<RModule, Error>where
T: IntoId,
Define a module in self
’s scope.
§Examples
use magnus::{define_module, prelude::*, rb_assert};
let outer = define_module("Outer").unwrap();
outer.define_module("Inner").unwrap();
rb_assert!("Outer::Inner.is_a?(Module)");
rb_assert!("!Outer::Inner.is_a?(Class)");
sourcefn define_error<T>(
self,
name: T,
superclass: ExceptionClass
) -> Result<ExceptionClass, Error>where
T: IntoId,
fn define_error<T>(
self,
name: T,
superclass: ExceptionClass
) -> Result<ExceptionClass, Error>where
T: IntoId,
Define an exception class in self
’s scope.
§Examples
use magnus::{define_module, exception, prelude::*, rb_assert};
let outer = define_module("Outer").unwrap();
outer
.define_error("InnerError", exception::standard_error())
.unwrap();
rb_assert!("Outer::InnerError.is_a?(Class)");
rb_assert!("Outer::InnerError < Exception");
sourcefn include_module(self, module: RModule) -> Result<(), Error>
fn include_module(self, module: RModule) -> Result<(), Error>
Include module
into self
.
Effectively makes module
the superclass of self
. See also
prepend_module
.
§Examples
use magnus::{class, function, prelude::*, rb_assert, RClass, RModule};
fn example() -> i64 {
42
}
let module = RModule::new();
module
.define_method("example", function!(example, 0))
.unwrap();
let class = RClass::new(class::object()).unwrap();
class.include_module(module).unwrap();
let obj = class.new_instance(()).unwrap();
rb_assert!("obj.example == 42", obj);
sourcefn prepend_module(self, module: RModule) -> Result<(), Error>
fn prepend_module(self, module: RModule) -> Result<(), Error>
Prepend self
with module
.
Similar to include_module
, but inserts
module
as if it were a subclass in the inheritance chain.
§Examples
use magnus::{call_super, eval, function, prelude::*, rb_assert, Error, RClass, RModule};
fn example() -> Result<i64, Error> {
Ok(call_super::<_, i64>(())? + 2)
}
let module = RModule::new();
module
.define_method("example", function!(example, 0))
.unwrap();
let class: RClass = eval(
r#"
class Example
def example
40
end
end
Example
"#,
)
.unwrap();
class.prepend_module(module).unwrap();
let obj = class.new_instance(()).unwrap();
rb_assert!("obj.example == 42", obj);
sourcefn const_set<T, U>(self, name: T, value: U) -> Result<(), Error>
fn const_set<T, U>(self, name: T, value: U) -> Result<(), Error>
Set the value for the constant name
within self
’s scope.
§Examples
use magnus::{class, rb_assert, Module};
class::array().const_set("EXAMPLE", 42).unwrap();
rb_assert!("Array::EXAMPLE == 42");
sourcefn const_get<T, U>(self, name: T) -> Result<U, Error>where
T: IntoId,
U: TryConvert,
fn const_get<T, U>(self, name: T) -> Result<U, Error>where
T: IntoId,
U: TryConvert,
Get the value for the constant name
within self
’s scope.
§Examples
use magnus::{class, eval, rb_assert, Module, RClass, Value};
eval::<Value>(
"
class Example
VALUE = 42
end
",
)
.unwrap();
let class = class::object().const_get::<_, RClass>("Example").unwrap();
rb_assert!("klass::VALUE == 42", klass = class);
sourcefn is_inherited<T>(self, other: T) -> bool
fn is_inherited<T>(self, other: T) -> bool
Returns whether or not self
inherits from other
.
Classes including a module are considered to inherit from that module.
§Examples
use magnus::{class, prelude::*, Module, RClass};
let a = RClass::new(class::object()).unwrap();
let b = RClass::new(a).unwrap();
assert!(b.is_inherited(a));
assert!(!a.is_inherited(b));
sourcefn ancestors(self) -> RArray
fn ancestors(self) -> RArray
Return the classes and modules self
inherits, includes, or prepends.
§Examples
use magnus::{class, rb_assert, Module};
let ary = class::string().ancestors();
rb_assert!(
"ary == [String, Comparable, Object, Kernel, BasicObject]",
ary,
);
sourcefn define_method<T, M>(self, name: T, func: M) -> Result<(), Error>
fn define_method<T, M>(self, name: T, func: M) -> Result<(), Error>
Define a method in self
’s scope.
§Examples
use magnus::{class, rb_assert, method, Module};
fn escape_unicode(s: String) -> String {
s.escape_unicode().to_string()
}
class::string()
.define_method("escape_unicode", method!(escape_unicode, 0))
.unwrap();
rb_assert!(
r#""🤖\etest".escape_unicode == "\\u{1f916}\\u{1b}\\u{74}\\u{65}\\u{73}\\u{74}""#,
);
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.
§Examples
use magnus::{class, eval, exception, function, rb_assert, 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))
.unwrap();
eval::<Value>(
r#"
class String
def percent_encode
chars.map {|c| percent_encode_char(c)}.join("")
end
end
"#,
)
.unwrap();
rb_assert!(r#""foo bar".percent_encode == "foo%20bar""#);
assert!(eval::<bool>(r#"" ".percent_encode_char(" ")"#)
.unwrap_err()
.is_kind_of(exception::no_method_error()));
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.
§Examples
use magnus::{class, eval, exception, method, rb_assert, 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))
.unwrap();
class::string()
.define_protected_method("invisible?", method!(is_invisible, 0))
.unwrap();
eval::<Value>(
r#"
class String
def escape_invisible
chars.map {|c| c.invisible? ? c.escape_unicode : c}.join("")
end
end
"#,
)
.unwrap();
rb_assert!(
r#"
"🤖\tfoo bar".escape_invisible == "🤖\\u{9}foo\\u{20}bar"
"#,
);
assert!(eval::<bool>(r#"" ".invisible?"#)
.unwrap_err()
.is_kind_of(exception::no_method_error()));
sourcefn define_attr<T>(self, name: T, rw: Attr) -> Result<(), Error>where
T: IntoId,
fn define_attr<T>(self, name: T, rw: Attr) -> Result<(), Error>where
T: IntoId,
Define public accessor methods for the attribute name
.
name
should be without the preceding @
.
§Examples
use magnus::{class, eval, prelude::*, rb_assert, Attr, Module, RClass, Value};
let class = RClass::new(class::object()).unwrap();
class.define_attr("example", Attr::ReadWrite).unwrap();
let obj = class.new_instance(()).unwrap();
let _: Value = eval!("obj.example = 42", obj).unwrap();
rb_assert!("obj.example == 42", obj);
sourcefn define_alias<T, U>(self, dst: T, src: U) -> Result<(), Error>
fn define_alias<T, U>(self, dst: T, src: U) -> Result<(), Error>
Alias the method src
of self
as dst
.
§Examples
use magnus::{class, function, prelude::*, rb_assert, Module, RClass};
fn example() -> i64 {
42
}
let class = RClass::new(class::object()).unwrap();
class
.define_method("example", function!(example, 0))
.unwrap();
class.define_alias("test", "example").unwrap();
let obj = class.new_instance(()).unwrap();
rb_assert!("obj.test == 42", obj);