Struct ruru::AnyObject [] [src]

pub struct AnyObject {
    // some fields omitted
}

Representation of any Ruby object while its type is unknown

As Ruby is a dynamically typed language, at some points Ruru does not know the exact Ruby type of the object. It happens in the following cases:

  • Retrieving an object from array

  • Retrieving an object from hash

  • Receiving arguments to callback

  • Initializing new instance of a class which is not present in Ruru

  • and some other places you can find in Ruru documentation

In these cases you should cast AnyObject to the type to which the object belongs using functions below.

Examples

Retrieving an object from Array

use ruru::{Array, Fixnum, VM};

let array = Array::new().push(Fixnum::new(1));
let value = array.at(0).as_fixnum(); // `Array::at()` returns `AnyObject`

assert_eq!(value.to_i64(), 1);

Retrieving an object from Hash

use ruru::{Fixnum, Hash, Symbol, VM};

let mut hash = Hash::new();

hash.store(Symbol::new("key"), Fixnum::new(1));

// `Hash::at()` returns `AnyObject`
let value = hash.at(Symbol::new("key")).as_fixnum();

assert_eq!(value, Fixnum::new(1));

Receiving arguments from Ruby to Ruru callback

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 {
    // `VM::parse_arguments()` returns `Vec<AnyObject>`
    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);
}

Methods

impl AnyObject
[src]

fn as_array(&self) -> Array

Casts to Array.

fn as_boolean(&self) -> Boolean

Casts to Boolean.

fn as_class(&self) -> Class

Casts to Class.

fn as_fixnum(&self) -> Fixnum

Casts to Fixnum.

fn as_hash(&self) -> Hash

Casts to Hash.

fn as_string(&self) -> RString

Casts to String.

fn as_symbol(&self) -> Symbol

Casts to Symbol.

Trait Implementations

impl Clone for AnyObject
[src]

fn clone(&self) -> AnyObject

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)
1.0.0

Performs copy-assignment from source. Read more

impl From<Value> for AnyObject
[src]

fn from(value: Value) -> Self

Performs the conversion.

impl Object for AnyObject
[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