| Class | SequencedHash |
| In: |
lib/collections/sequenced_hash.rb
|
| Parent: | Hash |
$Revision: 111 $
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!
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"
code.juretta.com/project/collections/
Stefan Saasen <s@juretta.com>
MIT
| key? | -> | contains_key? |
| seq | [R] |
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
Returns the element at index. A negative index counts from the end of self. Returns nil if the index is out of range.
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 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 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 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]]