core : hash

Hashtables, associative arrays, maps, dictionaries...whatever you want to call them, these are lists which can be indexed by an arbitrary value.
+   ==   clear   delete   each   each_value   empty?   get   hash?   key?   keys   length   map   new   select   set   to_s   values  

Manipulating Hashes

Hashes can be created using the following syntax:

a = ["a" : 1, 2 : 3]

They can also be indexed using the [] syntax:

a = ["a" : 1, 2 : 3]

a["a"]  #1
a[2]  #3

(hash) +

hash + hash2

Combines two hashes into a single hash. Values from the righthand side value take precedence.

(boolean) ==

hash == hash2

Returns true if the contents of the two hashes are the same.

(self) clear

hash.clear

Removes all contents from hash.

(object) delete

hash.delete key

Deletes given key from the hash table. Returns the value stored at that key.

(self) each

hash.each block

Invokes the block for each key-value pair in the hash.

(self) each_value

hash.each_value block

Invokes the block for each value in the hash.

(self) each_value

hash.each_value block

Invokes the block for each key in the hash.

(boolean) empty?

hash.empty?

Returns true if the hash table is empty.

(object) get

hash.get key

Returns the value stored at the given key. More commonly used with the [] syntax.

 h = [:]
 h[:hello] = :world
 h[:hello] # returns "world"

(boolean) hash?

hash.hash?

Returns true.

(boolean) key?

hash.key? key

Returns true if the hash table contains the given key.

(array) keys

hash.keys

Returns an array containing all the keys from the hash table.

(number) length

hash.length

Returns the number of elements in the hash table.

 [a: 1].length # 1

(array) map

hash.map block

Invokes the block for each key-value pair in the hash and returns a new array containing the results.

(hash) new

hash.new

Returns a new hash table.

(hash) select

hash.select block

Passes each key-value to the given block and returns a new hash only containing the pairs for which the block returns true.

(object) set

hash.set key, value

Stores the given value at the given key. More commonly used with the [] syntax.

 h = [:]
 h[:hello] = :world
 h[:hello] # returns "world"

(string) to_s

hash.to_s

Converts the hash table to a string.

(array) values

hash.values

Returns an array containing all the values from the hash table.

Fork me on GitHub