Struct ruru::Class [] [src]

pub struct Class {
    // some fields omitted
}

Class

Methods

impl Class
[src]

fn new(name: &str) -> Self

Creates a new Class.

Examples

use ruru::{Class, VM};

let class = Class::new("Hello");

assert_eq!(class, Class::from_existing("Hello"));

Ruby:

class Hello
end

# or

Hello = Class.new

fn from_existing(name: &str) -> Self

Retrieves an existing Class object.

Examples

use ruru::{Class, VM};

let class = Class::new("Hello");

assert_eq!(class, Class::from_existing("Hello"));

Ruby:

Hello

# or

Object.const_get('Hello')

fn new_instance(&self, arguments: Vec<AnyObject>) -> AnyObject

Creates a new instance of Class

Arguments must be passed as a vector of AnyObject (see example).

Examples

use ruru::{Class, Fixnum};
use ruru::traits::Object;

// Without arguments
Class::from_existing("Hello").new_instance(vec![]);

// With arguments passing arguments to constructor
let arguments = vec![
    Fixnum::new(1).as_any_object(),
    Fixnum::new(2).as_any_object()
];

Class::from_existing("Worker").new_instance(arguments);

Ruby:

Hello.new

Worker.new(1, 2)

fn define<F: Fn(&mut Self)>(&mut self, f: F) -> &Self

Wraps calls to a class.

Mostly used to have Ruby-like class definitions.

Examples

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

#[no_mangle]
pub extern fn greeting(_: Argc, _: *const AnyObject, _: AnyObject) -> RString {
    RString::new("Greeting from class")
}

#[no_mangle]
pub extern fn many_greetings(_: Argc, _: *const AnyObject, _: AnyObject) -> RString {
    RString::new("Many greetings from instance")
}

fn main() {
    Class::new("Hello").define(|itself| {
        itself.def_self("greeting", many_greetings);
        itself.def("many_greetings", greeting);
    });
}

Ruby:

class Hello
  def self.print_many_greetings
    'Hello from class'
  end

  def print_greeting
    'Hello from instance'
  end
end

fn define_method<I: Object, O: Object>(&mut self, name: &str, callback: Callback<I, O>)

Defines an instance method for given class.

Arguments

  • name is name of the Ruby method

  • callback is the function which will be called by MRI when the method is called inside Ruby

Callback

callback must have the following signature:

pub type Callback<I: Object, O: Object> = extern fn(Argc, *const AnyObject, I) -> O;

The function must also have #[no_mangle] attribute.

  • First argument argc: Argc will receive the number of arguments passed to method

  • Second argument argv: *const AnyObject will receive the arguments passed to the method as AnyObjects

  • Third value itself: I will receive the object which got the method call (Ruby self). Can be any type which implements Object trait

  • Return type can be any type which implements Object trait

If you need to receive and use arguments which are passed to the method, you can use VM::parse_arguments() function which processes a pointer to array (*const AnyObject) to a vector of AnyObjects (Vec<AnyObject>), see examples.

Examples

Method receives no arguments

In this case argc and argv can be ignored.

Famous String#blank? example

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

use ruru::traits::Object;

#[no_mangle]
pub extern fn string_blank(_: Argc, _: *const AnyObject, itself: RString) -> Boolean {
    Boolean::new(itself.to_string().chars().all(|c| c.is_whitespace()))
}

fn main() {
    Class::from_existing("String").define_method("blank?", string_blank);
}

Ruby:

class String
  def blank?
    # simplified
    self.chars.all? { |c| c == ' ' }
  end
end

Method receives arguments

Arguments should be processed to vector using VM::parse_arguments().

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].as_string();

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

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

Ruby:

class String
  def ==(other_string)
    # simplified
    self.chars == other_string.chars
  end
end

fn define_singleton_method<I: Object, O: Object>(&mut self, name: &str, callback: Callback<I, O>)

Defines a class method for given class.

Function has the same requirements as define_method. Also the same rules are applied for callback (see above).

Examples

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

#[no_mangle]
pub extern fn symbol_from_string(argc: Argc, argv: *const AnyObject, itself: Class) -> Symbol {
    let argv = VM::parse_arguments(argc, argv);
    let string = argv[0].as_string();

    Symbol::new(&string.to_string())
}

fn main() {
    Class::from_existing("Symbol")
        .define_singleton_method("from_string", symbol_from_string);
}

Ruby:

class Symbol
  def self.from_string(string)
    # simplified
    string.to_sym
  end
end

fn def<I: Object, O: Object>(&mut self, name: &str, callback: Callback<I, O>)

An alias for define_method (similar to Ruby syntax def some_method).

fn def_self<I: Object, O: Object>(&mut self, name: &str, callback: Callback<I, O>)

An alias for define_singleton_method (similar to Ruby def self.some_method).

Trait Implementations

impl PartialEq for Class
[src]

fn eq(&self, __arg_0: &Class) -> bool

This method tests for self and other values to be equal, and is used by ==. Read more

fn ne(&self, __arg_0: &Class) -> bool

This method tests for !=.

impl Debug for Class
[src]

fn fmt(&self, __arg_0: &mut Formatter) -> Result

Formats the value using the given formatter.

impl From<Value> for Class
[src]

fn from(value: Value) -> Self

Performs the conversion.

impl Object for Class
[src]

fn value(&self) -> Value

Usually this function just returns a value of current object. Read more

fn class(&self) -> Class

Returns a Class struct of current object Read more

fn send(&self, method: &str, arguments: Vec<AnyObject>) -> AnyObject

Calls a given method on an object similarly to Ruby Object#send method Read more

fn as_any_object(&self) -> AnyObject

Converts struct to AnyObject Read more

fn instance_variable_get(&self, variable: &str) -> AnyObject

Sets an instance variable for object Read more

fn instance_variable_set<T: Object>(&mut self, variable: &str, value: T) -> AnyObject

Gets an instance variable of object Read more