Struct ruru::Hash [] [src]

pub struct Hash {
    // some fields omitted
}

Hash

Methods

impl Hash
[src]

fn new() -> Self

Creates a new instance of empty Hash.

Examples

use ruru::{Hash, VM};

Hash::new();

Ruby:

{}

fn at<T: Object>(&self, key: T) -> AnyObject

Retrieves an AnyObject from element stored at key key.

Examples

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

let mut hash = Hash::new();

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

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

Ruby:

hash = {}
hash[:key] = 1

hash[:key] == 1

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

Associates the value with the key.

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

Examples

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

let mut hash = Hash::new();

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

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

Ruby:

hash = {}
hash[:key] = 1

hash[:key] == 1

fn each<K, V, F>(&self, closure: F) where K: Object, V: Object, F: FnMut(K, V)

Runs a closure for each key and value pair.

You can specify types for each object if they are known and the same for each key and each value:


hash.each(|key: Symbol, value: Fixnum| {
    // ...
});

If the types are unknown or may vary, use AnyObject type.

Examples

use ruru::{Fixnum, Hash, 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: Symbol, value: 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 From<Value> for Hash
[src]

fn from(value: Value) -> Self

Performs the conversion.

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