ruru::class
[−]
[src]
macro_rules! class { ($class: ident) => { ... }; }
Creates Rust structure for new Ruby class
This macro does not define an actual Ruby class. It only creates structs for using
the class in Rust. To define the class in Ruby, use Class
structure.
Examples
#[macro_use] extern crate ruru; use ruru::{Class, RString, Object, VM}; class!(Greeter); methods!( Greeter, itself, fn anonymous_greeting() -> RString { RString::new("Hello stranger!") } fn friendly_greeting(name: RString) -> RString { let name = name .map(|name| name.to_string()) .unwrap_or("Anonymous".to_string()); let greeting = format!("Hello dear {}!", name); RString::new(&greeting) } ); fn main() { Class::new("Greeter", None).define(|itself| { itself.def("anonymous_greeting", anonymous_greeting); itself.def("friendly_greeting", friendly_greeting); }); }
Ruby:
class Greeter
def anonymous_greeting
'Hello stranger!'
end
def friendly_greeting(name)
default_name = 'Anonymous'
name = defaut_name unless name.is_a?(String)
"Hello dear #{name}"
end
end