| Crates.io | macro-ruby |
| lib.rs | macro-ruby |
| version | 1.1.1 |
| created_at | 2021-10-06 21:30:20.394407+00 |
| updated_at | 2021-10-07 20:35:26.780984+00 |
| description | Execute ruby code at compile time trough mruby |
| homepage | |
| repository | https://github.com/DumbMahreeo/macro-ruby |
| max_upload_size | |
| id | 461376 |
| size | 22,867 |
A crate to generate rust code trough Ruby at compile time. (Tested with mruby 3.0.0)
Have mruby in your PATH.
use macro_ruby::ruby_code_str;
// ruby_code_str! generates a &str based on what has been printed from Ruby.
assert_eq!(
ruby_code_str!("puts 'hi'"),
"hi\n"
);
assert_eq!(
ruby_code_str!("print 'hi'"),
"hi"
);
use macro_ruby::ruby_code_to;
// ruby_code_to! generates a value which type is based off of input
// and the content on what has been printed from Ruby.
assert_eq!(
ruby_code_to!(i32 "print 500+500"),
1000
);
assert_eq!(
ruby_code_to!(u8 "print 500+500"), // Will panic because u8 overflows
1000
);
use macro_ruby::ruby_code_ast;
// ruby_code_ast! generates real rust code based on what has been printed
ruby_code_ast!(r#"
puts "let a = 1;"
"#)
assert_eq!(a, 1);
If you want to execute ruby code from external files you can use the file variant of our macros
| Str version | File version |
|---|---|
ruby_code_str! |
ruby_file_str! |
ruby_code_to! |
ruby_file_to! |
ruby_code_ast! |
ruby_file_ast! |
If you want to use YARV (the official Ruby interpreter) instead of mruby simply enable the "yarv" or "full" feature (yarv and full are aliases).
So in our cargo.toml we'll have
[dependencies]
macro-ruby = { version = current_version, features = ["yarv"] }
or
[dependencies]
macro-ruby = { version = current_version, features = ["full"] }
(where current_version is the actual current version of macro-ruby)