Struct rutie::Hash[][src]

pub struct Hash { /* fields omitted */ }

Hash

Implementations

impl Hash[src]

pub fn new() -> Self[src]

Creates a new instance of empty Hash.

Examples

use rutie::{Hash, VM};

Hash::new();

Ruby:

{}

pub fn at<T: Object>(&self, key: &T) -> AnyObject[src]

Retrieves an AnyObject from element stored at key key.

Examples

use rutie::{Fixnum, Hash, Object, Symbol, VM};

let mut hash = Hash::new();

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

assert_eq!(hash.at(&Symbol::new("key")).try_convert_to::<Fixnum>(), Ok(Fixnum::new(1)));

Ruby:

hash = {}
hash[:key] = 1

hash[:key] == 1

pub fn store<K: Object, V: Object>(&mut self, key: K, value: V) -> AnyObject[src]

Associates the value with the key.

Both key and value must be types which implement Object trait.

Examples

use rutie::{Fixnum, Hash, Object, Symbol, VM};

let mut hash = Hash::new();

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

assert_eq!(hash.at(&Symbol::new("key")).try_convert_to::<Fixnum>(), Ok(Fixnum::new(1)));

Ruby:

hash = {}
hash[:key] = 1

hash[:key] == 1

pub fn length(&self) -> usize[src]

Retrieves the length of the hash.

Examples

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

let mut hash = Hash::new();

hash.store(Symbol::new("key1"), Fixnum::new(1));
assert_eq!(hash.length(), 1);

hash.store(Symbol::new("key2"), Fixnum::new(2));
assert_eq!(hash.length(), 2);

Ruby:

hash = {}

hash[:key1] = 1
hash.length == 1

hash[:key2] = 2
hash.length == 2

pub fn clear(&self)[src]

Removes all key-value pairs.

Examples

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

let mut hash = Hash::new();

hash.store(Symbol::new("key1"), Fixnum::new(1));
hash.store(Symbol::new("key2"), Fixnum::new(2));
assert_eq!(hash.length(), 2);

hash.clear();
assert_eq!(hash.length(), 0);

Ruby:

hash = {}

hash[:key1] = 1
hash[:key2] = 2
hash.length == 2

hash.clear

hash.length == 0

pub fn delete<K: Object>(&mut self, key: K) -> AnyObject[src]

Deletes the key-value pair and returns the value from hash whose key is equal to key. If the key is not found, it returns nil.

key must be a type which implements the Object trait.

Examples

use rutie::{Fixnum, Hash, Object, Symbol, VM};

let mut hash = Hash::new();

hash.store(Symbol::new("key1"), Fixnum::new(1));
hash.store(Symbol::new("key2"), Fixnum::new(2));
assert_eq!(hash.length(), 2);

let deleted = hash.delete(Symbol::new("key2"));
assert_eq!(hash.length(), 1);
assert_eq!(deleted.try_convert_to::<Fixnum>(), Ok(Fixnum::new(2)));

Ruby:

hash = {}

hash[:key1] = 1
hash[:key2] = 2
hash.length == 2

deleted = hash.delete(:key2)

hash.length == 1
deleted == 2

pub fn each<F>(&self, closure: F) where
    F: FnMut(AnyObject, AnyObject), 
[src]

Runs a closure for each key and value pair.

Key and value have AnyObject type.

Examples

use rutie::{Fixnum, Hash, Object, Symbol, VM};

let mut hash = Hash::new();

hash.store(Symbol::new("first_key"), Fixnum::new(1));
hash.store(Symbol::new("second_key"), Fixnum::new(2));

let mut doubled_values: Vec<i64> = Vec::new();

hash.each(|_key, value| {
    if let Ok(value) = value.try_convert_to::<Fixnum>() {
        doubled_values.push(value.to_i64() * 2);
    }
});

assert_eq!(doubled_values, vec![2, 4]);

Ruby:

hash = {
  first_key: 1,
  second_key: 2
}

doubled_values = []

hash.each do |_key, value|
  doubled_values << [value * 2]
end

doubled_values == [2, 4]

Trait Implementations

impl Clone for Hash[src]

impl Debug for Hash[src]

impl Default for Hash[src]

impl From<Value> for Hash[src]

impl Into<AnyObject> for Hash[src]

impl Into<Value> for Hash[src]

impl Object for Hash[src]

impl PartialEq<Hash> for Hash[src]

impl VerifiedObject for Hash[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.