Struct ruru::VM [] [src]

pub struct VM;

Virtual Machine and helpers

Methods

impl VM
[src]

fn init()

Initializes Ruby virtual machine.

This function should ONLY be used if you write a standalone application which calls Ruby itself, for example:

  • Sidekiq-like background processing

  • Unicorn-like web server

In these cases it should be called before any interaction with Ruby.

If you write a library which is being connected to Ruby in runtime (e.g. some gem), this function should not be used.

Examples

use ruru::{Class, VM};

VM::init();

// VM started, able to use Ruby now
// ...

Class::new("SomeClass"); // etc

fn require(name: &str)

Requires Ruby source file.

Examples

use ruru::VM;

VM::require("some_ruby_file");

fn block_proc() -> Proc

Converts a block given to current method to a Proc

It works similarly to def method(&block) which converts block to Proc

Examples

#[macro_use]
extern crate ruru;

use ruru::{Class, Proc, RString, VM};
use ruru::traits::Object;

class!(Greeter);

methods!(
    Greeter,
    itself,

    fn greet_rust_with() -> RString {
        let greeting_template = VM::block_proc();
        let name = RString::new("Rust").to_any_object();

        greeting_template.call(vec![name]).to::<RString>()
    }
);

fn main() {
    Class::new("Greeter").define(|itself| {
        itself.def_self("greet_rust_with", greet_rust_with);
    });
}

Ruby:

class Greeter
  def self.greet_rust_with(greeting_template)
    greeting_template.call('Rust')
  end
end

Greeter.greet_rust_with do |name|
  "Hello, #{name}!"
end
# => "Hello, Rust!"

fn parse_arguments(argc: Argc, arguments: *const AnyObject) -> Vec<AnyObject>

Converts a pointer AnyObject array to Vec<AnyObject>.

This function is a helper for callbacks.

Later it will be moved to other struct, because it is not related to VM itself.

Examples

use ruru::types::Argc;
use ruru::{AnyObject, Boolean, Class, RString, VM};

#[no_mangle]
pub extern fn string_eq(argc: Argc, argv: *const AnyObject, itself: RString) -> Boolean {
    let argv = VM::parse_arguments(argc, argv);
    let other_string = argv[0].to::<RString>();

    Boolean::new(itself.to_string() == other_string.to_string())
}

fn main() {
    Class::from_existing("String").define_method("==", string_eq);
}