Struct rutie::AnyObject

source ·
pub struct AnyObject { /* private fields */ }
Expand description

Representation of any Ruby object while its type is unknown

As Ruby is a dynamically typed language, at some points Rutie does not know the exact Ruby type of the object, for example:

  • Retrieving an object from array;

  • Retrieving an object from hash;

  • Receiving arguments to a method;

  • Initializing a new instance of a non-built-in class.

In these cases you should cast AnyObject to the required type.

Examples

Retrieving an object from Array

use rutie::{Array, Fixnum, Object, VM};

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

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

Retrieving an object from Hash

use rutie::{Fixnum, Hash, Object, 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")).try_convert_to::<Fixnum>();

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

You can find more examples in Class, Object and VerifiedObject documentation.

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.

Converts an iterator into Array.

Examples

use rutie::{Array, Fixnum, Object, VM};

let array: Array = (1..6)
    .map(|num| num * 2)
    .map(|num| Fixnum::new(num).to_any_object())
    .collect();

assert_eq!(array.length(), 5);

for i in 0..5 {
    let expected_number = (i + 1) * 2;

    assert_eq!(array.at(i).try_convert_to::<Fixnum>().unwrap().to_i64(), expected_number);
}
Creates a value from an iterator. Read more
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Converts this type into the (usually inferred) input type.
Returns internal value of current object. Read more
Returns a class of current object. Read more
Returns a singleton class of current object. Read more
Gets an immutable reference to the Rust structure which is wrapped into a Ruby object. Read more
Gets a mutable reference to the Rust structure which is wrapped into a Ruby object.
Wraps calls to the object. Read more
Defines an instance method for the given class or object. Read more
Defines a private instance method for the given class or object. Read more
Defines a class method for given class or singleton method for object. Read more
An alias for define_method (similar to Ruby syntax def some_method).
An alias for define_private_method (similar to Ruby syntax private def some_method).
An alias for define_singleton_method (similar to Ruby def self.some_method).
Calls a given method on an object similarly to Ruby Object#send method Read more
Alias for Ruby’s == Read more
Alias for Ruby’s === Read more
Alias for Ruby’s eql? Read more
Alias for Ruby’s equal? Read more
Checks whether the object responds to given method Read more
protect_send returns Result<AnyObject, AnyObject> Read more
protect_public_send returns Result<AnyObject, AnyObject> Read more
Checks whether the object is nil Read more
Converts struct to AnyObject Read more
Gets an instance variable of object Read more
Sets an instance variable for object Read more
Returns the freeze status of the object. Read more
Prevents further modifications to the object. Read more
Unsafely casts current object to the specified Ruby type Read more
Safely casts current object to the specified Ruby type Read more
Determines the value type of the object Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
The type returned in the event of a conversion error.
Performs the conversion.

Implicit or nil conversion

Examples

use rutie::{RString, Fixnum, VM, TryConvert, NilClass, Object};

let four = Fixnum::new(4);
let result = RString::try_convert(four.to_any_object());

assert_eq!(result, Err(NilClass::new()));

let five = RString::new_utf8("5");
let result2 = RString::try_convert(five.to_any_object());

if let Ok(r) = result2 {
  assert_eq!(r.to_str(), "5")
} else {
  unreachable!()
}

Ruby:

four = 4
result = String.try_convert(four)

result == nil

five = "5"
result = String.try_convert(five)

result == "5"
The type returned in the event of a conversion error.
Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.