Class SequencedHash
In: lib/collections/sequenced_hash.rb
Parent: Hash

SequencedHash

Version

 $Revision: 111 $

Description

A Map of objects whose mapping entries are sequenced based on the order in which they were added.

SequencedHash extends Hash so every method available in Hash is available here too!

Usage

 require 'rubygems'
 require 'collections'
 # or
 require 'collections/sequenced_hash'
 hash = SequencedHash.new
 hash[:v1] = "v1"
 hash[:v2] = "v2"
 hash[:v3] = "v3"
 hash.inspect # => {:v1=>"v1", :v2=>"v2", :v3=>"v3"}
 hash.at(0) # => "v1"
 hash.at(1) # => "v2"

Source

code.juretta.com/project/collections/

Author

 Stefan Saasen <s@juretta.com>

Licence

 MIT

Methods

<=>   ==   []=   add   at   clear   delete   delete_if   each   each_key   each_pair   each_value   eql?   first   grep   inspect   invert   keys   last   new   put   select   store   to_a   values  

External Aliases

key? -> contains_key?

Attributes

seq  [R] 

Public Class methods

SequencedHashes have a default value that is returned when accessing keys that do not exist in the hash. By default, that value is

 nil
 Hash.new                          => hash
 Hash.new(obj)                     => aHash
 Hash.new {|hash, key| block }     => aHash

Public Instance methods

[]=(key, value)

Alias for store

add(key, value)

Alias for store

Returns the element at index. A negative index counts from the end of self. Returns nil if the index is out of range.

Removes all key/value pairs from SequencedHash

Deletes and returns a key-value pair from hsh whose key is equal to key. If the key is not found, returns the _default value_. If the optional code block is given and the key is not found, pass in the key and return the result of block.

Deletes every key-value pair from hsh for which block evaluates to true.

 h = { "a" => 100, "b" => 200, "c" => 300 }
 h.delete_if {|key, value| key >= "b" }   #=> {"a"=>100}

Calls block once for each key in SequencedHash, passing the key and value as parameter.

Calls block for each key in SequencedHash, passing the key as the parameter.

each_pair()

Alias for each

Calls block for each key in SequencedHash, passing the value as the parameter.

Equality—At the Object level, +==+ returns true only if obj and other are the same object. Typically, this method is overridden in descendent classes to provide class-specific meaning.

Unlike +==+, the +equal?+ method should never be overridden by subclasses: it is used to determine object identity (that is, +a.equal?(b)+ iff a is the same object as b).

The +eql?+ method returns true if obj and anObject have the same value. Used by Hash to test members for equality. For objects of class Object, +eql?+ is synonymous with +==+. Subclasses normally continue this tradition, but there are exceptions. Numeric types, for example, perform type conversion across +==+, but not across +eql?+, so:

 1 == 1.0     #=> true
 1.eql? 1.0   #=> false

Returns the first element, or the first n elements, of the array. If the array is empty, the first form returns nil, and the second form returns an empty array.

Returns a nested array with key, value pairs for which +Pattern === key+ or +Pattern === value+. If the optional block is supplied, each matching key, value pair (as an array) is passed to it, and the block‘s result is stored in the output array.

 hash = SequencedHash.new
 hash["guitar"] = "Paul Reed Smith"
 hash["computer"] = "Mac Book Pro"
 hash["car"] = "Land Rover Defender"
 hash["ski"] = "Rossignol"
 hash["chute"] = "Electrar 170"
 h.grep(/(t|r)ar/)  # => [["guitar", "Paul Reed Smith"], ["chute", "Electrar 170"]]

 hash.grep(pattern)                   => array
 hash.grep(pattern) {| obj | block }  => array

Returns a new SequencedHash created by using SequencedHash‘s values as keys, and the keys as values.

Returns the last element(s) of self. If the array is empty, the first form returns nil.

put(key, value)

Alias for store

Returns a new array consisting of +[key,value]+ pairs for which the block returns true. Also see +Hash.values_at+.

 h = SequencedHash.new
 h["a"] = 100
 h["b"] = 200
 h["c"] = 300

 h.select {|k,v| k > "a"}  #=> [["b", 200], ["c", 300]]
 h.select {|k,v| v < 200}  #=> [["a", 100]]

Element assignment - Associates the value given by value with the key given by key

Converts SequencedHash to a nested array of +[+ _key, value_ +]+ arrays.

[Validate]