Have you ever considered rewriting some parts of your ~~slow~~ Ruby application?
Just rewrite your Ruby application with Rust method by method, class by class. It does not require to change interface of your classes or to change any other Ruby code.
As simple as Ruby, as efficient as Rust.
String#blank?
method```rust
pub extern fn isblank(: Argc, : *const AnyObject, itself: RString) -> Boolean { Boolean::new(itself.tostring().chars().all(|c| c.is_whitespace())) }
fn main() { Class::fromexisting("String").define(|itself| { itself.def("blank?", isblank); }); } ```
Let's say you have a Calculator
class.
```ruby class Calculator def pow3(number) (1..number).eachwith_object({}) do |index, hash| hash[index] = index ** 3 end end end
Calculator.new.pow_3(5) #=> { 1 => 1, 2 => 8, 3 => 27, 4 => 64, 5 => 125 } ```
You found it's very slow to call pow_3
for big number and decided to replace the whole class
with Rust.
```rust
pub extern fn pow3(argc: Argc, argv: *const AnyObject, itself: Fixnum) -> Hash { let argv = VM::parsearguments(argc, argv); let num = argv[0].asfixnum().toi64();
let mut hash = Hash::new();
for i in (1..num + 1) {
hash.store(Fixnum::new(i), Fixnum::new(i.pow(3)));
}
hash
}
Class::new("Calculator").define(|itself| { itself.def("pow3", pow3); }); ```
Ruby:
```ruby
Calculator.new.pow_3(5) #=> { 1 => 1, 2 => 8, 3 => 27, 4 => 64, 5 => 125 } ```
So nothing has changed in the API of class thus no need to change code elsewhere in the app.
If the Calculator
class from the example above has more methods Ruby methods, but we want to
replace only pow_3
, use Class::from_existing()
rust
Class::from_existing("Calculator").define(|itself| {
itself.def("pow_3", pow_3);
});
Getting an account balance of some User
whose name is John and who is 18 or 19 years old.
ruby
User
.find_by(age: [18, 19], name: 'John')
.account_balance
```rust let mut conditions = Hash::new();
conditions.store( Symbol::new("age"), Array::new().push(Fixnum::new(18)).push(Fixnum::new(19)) );
conditions.store( Symbol::new("name"), RString::new("John") );
let accountbalance = Class::fromexisting("User") .send("findby", vec![conditions.asanyobject()]) .send("accountbalance") .asfixnum() .toi64(); ```
No support of native Ruby types;
No way to create a standalone application to run Ruby VM separately;
No way to call your Ruby code from Rust;
Ruru requires rbenv
to be installed. Building and linking process is automatic.
There are two ways of using Ruru:
Standalone application - Rust is run first as a compiled executable file and then it calls Ruby
code (see docs for VM::init()
)
Running Rust code from Ruby application
The second way requires additional steps (to be improved):
Add Ruru to Cargo.toml
toml
[dependencies]
ruru = ">= 0.5.0"
Compile your library as dylib
toml
[lib]
crate-type = ["dylib"]
Create a function which will initialize
```rust
pub extern fn initializemyapp() { Class::new("SomeClass");
/// ... etc } ```
Open the library and call function from Ruby
```ruby require 'fiddle'
library = Fiddle::dlopen('libmy_library.dylib')
Fiddle::Function.new(library['initmyapp'], [], Fiddle::TYPE_VOIDP).call ```
Ruru is ready :heart: