"value". A Hash is similar to an Array, except that its index is not limited to using numbers. The index..."> "value". A Hash is similar to an Array, except that its index is not limited to using numbers. The index...">

YouTip LogoYouTip

Ruby Hash

A Hash is a collection of key-value pairs like "key" => "value". A Hash is similar to an Array, except that its index is not limited to using numbers. The index (or "key") of a Hash can be almost any object. Although a Hash is similar to an Array, it has one very important difference: Hash elements have no specific order. If order is important, you should use an Array. Just like Arrays, there are various ways to create a Hash. You can create an empty Hash using the _new_ class method: months = Hash.new You can also create a Hash with a default value using _new_. A Hash without a default value is _nil_: months = Hash.new("month") or months = Hash.new"month" When you access any key in a Hash with a default value, if the key or value does not exist, accessing the Hash will return the default value: ## Example months = Hash.new("month")puts"#{months}"puts"#{months}" [Try it Β»](#) The output of the above example is: month month ## Example H = Hash["a" =>100, "b" =>200]puts"#{H['a']}"puts"#{H['b']}" [Try it Β»](#) The output of the above example is: 100200 You can use any Ruby object as a key or value, even an Array, as shown in the following example: [1,"jan"] =>"January" To call Hash methods, you first need to instantiate a Hash object. Here is how to create a Hash object instance: Hash[[key =>|, value]* ]or Hash.newHash.new(obj)Hash.new { |hash, key| block } This returns a new Hash populated with the given objects. Now, using the created object, we can call any available methods. For example: ## Example $, = ", "months = Hash.new("month")months = {"1" =>"January", "2" =>"February"} keys = months.keys puts"#{keys}" [Try it Β»](#) The output of the above example is: ["1", "2"] Below are the common Hash methods (assuming _hash_ is a Hash object): | No. | Method & Description | | --- | --- | | 1 | **hash == other_hash** Checks if two hashes have the same number of key-value pairs and if the key-value pairs match each other to determine if the two hashes are equal. | | 2 | **hash** Uses the key to reference a value from the hash. If the key is not found, returns the default value. | | 3 | **hash=value** Associates the given _value_ with the given _key_. | | 4 | **hash.clear** Removes all key-value pairs from the hash. | | 5 | **hash.default(key = nil)** Returns the default value of _hash_, or nil if not set via default=. (If the key does not exist in _hash_, [] returns a default value.) | | 6 | **hash.default = obj** Sets the default value for _hash_. | | 7 | **hash.default_proc** If _hash_ was created with a block, returns the block. | | 8 | **hash.delete(key) array.delete(key) { |key| block }** Deletes the key-value pair from _hash_ by _key_. If a block is used and no matching key-value pair is found, returns the result of the block. Compare this with _delete_if_. | | 9 | **hash.delete_if { |key,value| block }** Deletes every key-value pair from _hash_ for which the block returns _true_. | | 10 | **hash.each { |key,value| block }** Iterates over _hash_, calling the block once for each _key_, passing key-value as a two-element array. | | 11 | **hash.each_key { |key| block }** Iterates over _hash_, calling the block once for each _key_, passing _key_ as an argument. | | 12 | **hash.each_key { |key_value_array| block }** Iterates over _hash_, calling the block once for each _key_, passing _key_ and _value_ as arguments. | | 13 | **hash.each_value { |value| block }** Iterates over _hash_, calling the block once for each _key_, passing _value_ as an argument. | | 14 | **hash.empty?** Checks if the hash is empty (contains no key-value pairs), returning _true_ or _false_. | | 15 | **hash.fetch(key [, default] ) hash.fetch(key) { | key | block }** Returns a value from the hash for the given _key_. If the _key_ is not found and no other arguments are given, raises an _IndexError_ exception; if _default_ is given, returns _default_; if the optional block is specified, returns the result of the block. | | 16 | **hash.has_key?(key) hash.include?(key) hash.key?(key) hash.member?(key)** Checks if the given _key_ exists in the hash, returning _true_ or _false_. | | 17 | **hash.has_value?(value)** Checks if the hash contains the given _value_. | | 18 | **hash.index(value)** Returns the _key_ in the hash for the given _value_, or _nil_ if no matching value is found. | | 19 | **hash.indexes(keys)** Returns a new array consisting of values for the given keys. For keys not found, the default value is inserted. This method is deprecated; use select. | | 20 | **hash.indices(keys)** Returns a new array consisting of values for the given keys. For keys not found, the default value is inserted. This method is deprecated; use select. | | 21 | **hash.inspect** Returns a string representation of the hash. | | 22 | **hash.invert** Creates a new _hash_, inverting the _keys_ and _values_ of _hash_. That is, in the new hash, the keys from _hash_ become values, and the values become keys. | | 23 | **hash.keys** Creates a new array with the keys from _hash_. | | 24 | **hash.length** Returns the size or length of _hash_ as an integer. | | 25 | **hash.merge(other_hash) hash.merge(other_hash) { |key, oldval, newval| block }** Returns a new hash containing the contents of _hash_ and _other_hash_, overwriting key-value pairs in _hash_ with duplicate keys from _other_hash_. | | 26 | **hash.merge!(other_hash) hash.merge!(other_hash) { |key, oldval, newval| block }** Same as merge, but the hash itself is modified. | | 27 | **hash.rehash** Rebuilds the hash based on the current values of each _key_. If values have changed since insertion, this method re-indexes _hash_. | | 28 | **hash.reject { |key, value| block }** Similar to delete_if, but works on a copy of the hash. Equivalent to hsh.dup.delete_if. | | 29 | **hash.reject! { |key, value| block }** Equivalent to delete_if, but returns nil if no changes were made. | | 30 | **hash.replace(other_hash)** Replaces the contents of _hash_ with the contents of _other_hash_. | | 31 | **hash.select { |key, value| block }** Returns a new array consisting of key-value pairs from _hash_ for which the _block_ returns _true_. | | 32 | **hash.shift** Removes a key-value pair from _hash_ and returns it as a two-element array. | | 33 | **hash.size** Returns the _size_ or length of _hash_ as an integer. | | 34 | **hash.sort** Converts _hash_ to a two-dimensional array containing key-value pairs, then sorts it. | | 35 | **hash.store(key, value)** Stores a key-value pair in _hash_. | | 36 | **hash.to_a** Creates a two-dimensional array from _hash_. Each key-value pair is converted to an array, and all these arrays are stored in an array. | | 37 | **hash.to_hash** Returns _hash_ (self). | | 38 | **hash.to_s** Converts _hash_ to an array, then converts that array to a string. | | 39 | **hash.update(other_hash) hash.update(other_hash) {|key, oldval, newval| block}** Returns a new hash containing the contents of _hash_ and _other_hash_, overwriting key-value pairs in _hash_ with duplicate keys from _other_hash_. | | 40 | **hash.value?(value)** Checks if _hash_ contains the given _value_. | | 41 | **hash.values** Returns a new array containing all the values of _hash_. | | 42 | **hash.values_at(obj, ...)** Returns a new array containing the values in _hash_ associated with the given keys. |
← Ruby Date TimeMet Dialog Showmodal β†’