class Hash
back into that module.
When ‘Enumerable` gets implemented, the relevant methods will be moved
relevant methods used by `Hash` are implemented directly into this class.
The `Enumerable` module is not yet implemented in opal, so most of the
constantly.
library, and those that are not implemented are being added
`Hash` implements the majority of methods from the ruby standard
——————
Ruby compatibility
an external javascript library.
`Hash` instances, so those should be used if you need to use objects from
Hash and the JSON gem contain methods for converting native objects into
this method table is not found.
ruby object, like a javascript object, errors will start occuring when
which is their method table. When trying to send a message to a non
ruby hash. All ruby objects require, at minimum, a `.$m` property
different, and a javascript/json object cannot be used in place of a
javascript, it is very important to know that they are completely
Although its syntax is similar to that of an object literal in
construction through literals and methods such as {Hash.new}.
just an implementation detail, and is only done for the convenience of
prototype called RHash. This is all handled internally and is actually
An Opal hash is actually toll-free bridged to a custom javascript
———————-
Implementation details
exist in the hash. By default, that valus is `nil`.
Hashes have a default value that is returned when accessing keys that do not
were inserted.
index. Hahses enumerate their values in the order that the corresponding keys
that indexing is done via arbitrary keys of any object type, not an integer
A `Hash` is a collection of key-value pairs. It is similar to an array except
def self.[](*args)
-
(Hash)
-
def self.[](*args) `return $rb.H.apply(null, args);` end
def self.allocate
def self.allocate `return $rb.H();` end
def ==(other)
-
(true, false)
-
Parameters:
-
other
(Hash
) -- the hash to compare with
def ==(other) `if (self === other) return Qtrue; if (!other.$keys || !other.$assocs) return Qfalse; if (self.$keys.length != other.$keys.length) return Qfalse; for (var i = 0; i < self.$keys.length; i++) { var key1 = self.$keys[i], assoc1 = key1.$hash(); if (!other.$assocs.hasOwnProperty(assoc1)) return Qfalse; var assoc2 = other.$assocs[assoc1]; if (!#{`self.$assocs[assoc1]` == `assoc2`}.$r) return Qfalse; } return Qtrue;` end
def [](key)
-
(Object)
- result or default value
Parameters:
-
key
(Object
) -- the key to look for
def [](key) `var assoc = key.$hash(); if (self.$assocs.hasOwnProperty(assoc)) return self.$assocs[assoc]; return self.$default;` end
def []=(key, value)
-
(Object)
- returns the set value
Parameters:
-
value
(Object
) -- the value for the key -
key
(Object
) -- the key for hash
def []=(key, value) `var assoc = key.$hash(); if (!self.$assocs.hasOwnProperty(assoc)) self.$keys.push(key); return self.$assocs[assoc] = value;` end
def assoc(obj)
-
(Array
- result or nil
Parameters:
-
obj
(Object
) -- key to search for
def assoc(obj) `var key, keys = self.$keys, length = keys.length; for (var i = 0; i < length; i++) { key = keys[i]; if (#{`key` == obj}.$r) { return [key, self.$assocs[key.$hash()]]; } } return nil;` end
def clear
-
(Hash)
-
def clear `self.$keys = []; self.$assocs = {}; return self;` end
def default
def default `return self.$default;` end
def default=(obj)
-
(Object)
- returns the new default
Parameters:
-
obj
(Object
) -- the new default
def default=(obj) `return self.$default = obj;` end
def delete(key)
-
key
(Object
) -- the key to delete
def delete(key) `var assoc = key.$hash(); if (self.$assocs.hasOwnProperty(assoc)) { var ret = self.$assocs[assoc]; delete self.$assocs[assoc]; self.$keys.splice(self.$keys.indexOf(key), 1); return ret; } return self.$default;` end
def delete_if
-
(Hash)
- returns the receiver
def delete_if `var key, value; for (var i = 0; i < self.$keys.length; i++) { try { key = self.$keys[i]; value = self.$assocs[key.$hash()]; if (#{yield `key`, `value`}.$r) { delete self.$assocs[key.$hash()]; self.$keys.splice(i, 1); i--; } } catch (e) { switch (e.$keyword) { case 2: return e['@exit_value']; default: throw e; } } } return self;` end
def each
-
(Hash)
- returns the receiver
def each `var keys = self.$keys, values = self.$assocs, length = keys.length, key; for (var i = 0; i < length; i++) { try { key = keys[i]; #{yield `key`, `values[key.$hash()]`}; } catch (e) { switch (e.$keyword) { case 2: return e['@exit_value']; default: throw e; } } } return self;` end
def each_key
-
(Hash)
- returns receiver
def each_key `var key; for (var i = 0; i < self.$keys.length; i++) { try { key = self.$keys[i]; #{yield `key`}; } catch (e) { switch (e.$keyword) { case 2: return e['@exit_value']; default: throw e; } } } return self;` end
def each_value
-
(Hash)
- returns the receiver
def each_value `var val; for (var i = 0; i < self.$keys.length; i++) { try { val = self.$assocs[self.$keys[i].$hash()]; #{yield `val`}; } catch (e) { switch (e.$keyword) { case 2: return e['@exit_value']; default: throw e; } } } return self;` end
def empty?
-
(true, false)
-
def empty? `return self.$keys.length == 0 ? Qtrue : Qfalse;` end
def fetch(key, defaults = `undefined`)
-
(Object)
- value from hash
Parameters:
-
defaults
(Object
) -- the default value to return -
key
(Object
) -- the key to lookup
def fetch(key, defaults = `undefined`) `var value = self.$assocs[key.$hash()]; if (value != undefined) return value; else if (defaults == undefined) rb_raise('KeyError: key not found'); else return defaults;` end
def flatten(level = 1)
-
(Array)
- flattened hash
Parameters:
-
level
(Numeric
) -- the level to flatten until
def flatten(level = 1) `var result = [], key, value; for (var i = 0; i < self.$keys.length; i++) { key = self.$keys[i]; value = self.$assocs[key.$hash()]; result.push(key); if (value instanceof Array) { if (level == 1) { result.push(value); } else { var tmp = #{`value`.flatten `level - 1`}; result = result.concat(tmp); } } else { result.push(value); } } return result;` end
def has_key?(key)
-
(true, false)
-
Parameters:
-
key
(Object
) -- the key to check
def has_key?(key) `if (self.$assocs.hasOwnProperty(key.$hash())) return Qtrue; return Qfalse;` end
def has_value?(value)
-
(true, false)
-
Parameters:
-
value
(Object
) -- the value to check for
def has_value?(value) `var key, value; for (var i = 0; i < self.$keys.length; i++) { key = self.$keys[i]; val = self.$assocs[key.$hash()]; if (#{`value` == `val`}.$r) return Qtrue; } return Qfalse;` end
def inspect
-
(String)
-
def inspect `var description = [], key, value; for (var i = 0; i < self.$keys.length; i++) { key = self.$keys[i]; value = self.$assocs[key.$hash()]; description.push(#{`key`.inspect} + '=>' + #{`value`.inspect}); } return '{' + description.join(', ') + '}';` end
def invert
-
(Hash)
- inverted hash
def invert end
def key(value)
-
(Object)
- key or nil
Parameters:
-
value
(Object
) -- the value to check for
def key(value) `var key, val; for (var i = 0; i < self.$keys.length; i++) { key = self.$keys[i]; val = self.$assocs[key.$hash()]; if (#{`value` == `val`}.$r) { return key; } } return nil;` end
def keys
-
(Array)
-
def keys `return self.$keys.slice(0);` end
def length
-
(Numeric)
-
def length `return self.$keys.length;` end
def merge(other)
-
other
(Hash
) -- hash to merge with
def merge(other) `var result = $opal.H() , key, val; for (var i = 0; i < self.$keys.length; i++) { key = self.$keys[i], val = self.$assocs[key.$hash()]; result.$keys.push(key); result.$assocs[key.$hash()] = val; } for (var i = 0; i < other.$keys.length; i++) { key = other.$keys[i], val = other.$assocs[key.$hash()]; if (!result.$assocs.hasOwnProperty(key.$hash())) { result.$keys.push(key); } result.$assocs[key.$hash()] = val; } return result;` end
def merge!(other)
-
(Hash)
-
Parameters:
-
other
(Hash
) --
def merge!(other) `var key, val; for (var i = 0; i < other.$keys.length; i++) { key = other.$keys[i]; val = other.$assocs[key.$hash()]; if (!self.$assocs.hasOwnProperty(key.$hash())) { self.$keys.push(key); } self.$assocs[key.$hash()] = val; } return self;` end
def rassoc(obj)
-
(Array)
-
Parameters:
-
obj
(Object
) -- object to check
def rassoc(obj) `var key, val; for (var i = 0; i < self.$keys.length; i++) { key = self.$keys[i]; val = self.$assocs[key.$hash()]; if (#{`val` == obj}.$r) return [key, val]; } return nil;` end
def replace(other)
-
(Hash)
- returns receiver
Parameters:
-
other
(Hash
) -- hash to replace with
def replace(other) `self.$keys = []; self.$assocs = {}; for (var i = 0; i < other.$keys.length; i++) { var key = other.$keys[i]; var val = other.$assocs[key.$hash()]; self.$keys.push(key); self.$assocs[key.$hash()] = val; } return self;` end
def shift
-
(Array, Object)
-
def shift `var key, val; if (self.$keys.length > 0) { key = self.$keys[0]; val = self.$assocs[key.$hash()]; self.$keys.shift(); delete self.$assocs[key.$hash()]; return [key, val]; } return self.$default;` end
def to_a
-
(Array)
-
def to_a `var result = [], key, value; for (var i = 0; i < self.$keys.length; i++) { key = self.$keys[i]; value = self.$assocs[key.$hash()]; result.push([key, value]); } return result;` end
def to_hash
-
(Hash)
-
def to_hash self end
def to_s
-
(String)
-
def to_s `var description = [], key, value; for (var i = 0; i < self.$keys.length; i++) { key = self.$keys[i]; value = self.$assocs[key.$hash()]; description.push(#{`key`.inspect} + #{`value`.inspect}); } return description.join('');` end
def values
-
(Array)
-
def values `var result = [], length = self.$keys.length; for (var i = 0; i < length; i++) { result.push(self.$assocs[self.$keys[i].$hash()]); } return result;` end