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).to::<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")).to::<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].to::<RString>(); Boolean::new(itself.to_string() == other_string.to_string()) } fn main() { Class::from_existing("String").define_method("==", string_eq); }
Methods
impl AnyObject
[src]
fn to<T: Object>(&self) -> T
Casts AnyObject
to the specified Ruby type
Examples
use ruru::{AnyObject, Fixnum, VM}; use ruru::traits::Object; let fixnum_as_any_object = Fixnum::new(1).to_any_object(); let fixnum = fixnum_as_any_object.to::<Fixnum>(); assert_eq!(fixnum.to_i64(), 1);
fn ty(&self) -> ValueType
Determines the value type of the object
Example
use ruru::{AnyObject, Fixnum, VM}; use ruru::traits::Object; use ruru::types::ValueType; let any_object = Fixnum::new(1).to_any_object(); assert_eq!(any_object.ty(), ValueType::Fixnum);
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]
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 is_nil(&self) -> bool
Checks weather the object is nil
Read more
fn to_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