1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
use types::Value; use super::array::Array; use super::boolean::Boolean; use super::class::Class; use super::fixnum::Fixnum; use super::hash::Hash; use super::string::RString; use super::symbol::Symbol; use super::traits::Object; /// 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}; /// # VM::init(); /// /// 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` /// /// ```no_run /// use ruru::{Fixnum, Hash, Symbol, VM}; /// # VM::init(); /// /// 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 /// /// ```no_run /// 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); /// } /// ``` #[derive(Clone)] pub struct AnyObject { value: Value } impl AnyObject { /// Casts to `Array`. pub fn as_array(&self) -> Array { Array::from(self.value) } /// Casts to `Boolean`. pub fn as_boolean(&self) -> Boolean { Boolean::from(self.value) } /// Casts to `Class`. pub fn as_class(&self) -> Class { Class::from(self.value) } /// Casts to `Fixnum`. pub fn as_fixnum(&self) -> Fixnum { Fixnum::from(self.value) } /// Casts to `Hash`. pub fn as_hash(&self) -> Hash { Hash::from(self.value) } /// Casts to `String`. pub fn as_string(&self) -> RString { RString::from(self.value) } /// Casts to `Symbol`. pub fn as_symbol(&self) -> Symbol { Symbol::from(self.value) } } impl From<Value> for AnyObject { fn from(value: Value) -> Self { AnyObject { value: value } } } impl Object for AnyObject { fn value(&self) -> Value { self.value } }