SUPPORT THE SITE WITH A CLICK

Subscribe Rss:

SUPPORT THE SITE WITH A CLICK

Friday, May 15, 2009

Working with Hashes

A Hash is a collection of key-value pairs. It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. The order in which you traverse a hash by either key or value may seem arbitrary, and will generally not be in the insertion order.

Deleting Key/Value Pairs


Key/value pairs of a Hash object can be deleted using clear, delete, delete_if, reject, reject!, and shift.
Use clear to remove all key/value pairs. This is essentially the same as assigning a new empty hash, but it's marginally faster.
Use shift to remove an unspecified key/value pair. This method returns the pair as a two-element array (or nil if no keys are left):
irb(main):004:0> a={5=>6,7=>8}
=> {5=>6, 7=>8}
irb(main):005:0> b=a.shift
=> [5, 6]
irb(main):006:0> a
=> {7=>8}
irb(main):007:0>

Use delete to remove a specific key/value pair. It accepts a key and returns the value associated with the key removed (if found). If the key is not found, the default value is returned.
irb(main):012:0> a={4=>4,7=>8,9=>10}
=> {7=>8, 9=>10, 4=>4}
irb(main):013:0> a.delete(7)
=> 8
irb(main):014:0> a
=> {9=>10, 4=>4}
irb(main):015:0> a.delete(5)
=> nil
irb(main):016:0> a.delete(6)
=> nil
irb(main):017:0> a.delete(6){"currently not available"}
=> "currently not available"
irb(main):018:0>

Iterating over a Hash


The Hash class has the standard iterator each.Calls block once for each key in hash, passing the key and value to the block as a two-element array.It also has each_key, each_pair, and each_value (each_pair is an alias for each).
irb(main):018:0> h = { "a" => 100, "b" => 200 }
=> {"a"=>100, "b"=>200}
irb(main):019:0> h.each {|key, value| puts "#{key} is #{value}" }
a is 100
b is 200
=> {"a"=>100, "b"=>200}

irb(main):020:0>   h = { "a" => 100, "b" => 200 }
=> {"a"=>100, "b"=>200}
irb(main):021:0> h.each_key {|key| puts key }
a
b
=> {"a"=>100, "b"=>200}

irb(main):022:0> h = { "a" => 100, "b" => 200 }
=> {"a"=>100, "b"=>200}
irb(main):023:0> h.each_pair {|key, value| puts "#{key} is #{value}" }
a is 100
b is 200
=> {"a"=>100, "b"=>200}

irb(main):024:0> {"a"=>3,"b"=>2}.each_value do |value|
irb(main):025:1* print "val = #{value};"
irb(main):026:1> end
val = 3;val = 2;=> {"a"=>3, "b"=>2}

Detecting Keys and Values in a Hash


Determining whether a key has been assigned can be done with has_key? or any one of its aliases: include?, key?, or member?.
irb(main):027:0> h = { "a" => 100, "b" => 200 }
=> {"a"=>100, "b"=>200}
irb(main):028:0> h.has_key?("a") #=> true
=> true
irb(main):029:0> h.has_key?("z") #=> false
=> false
irb(main):030:0> h.include?("a")
=> true
irb(main):031:0> h.include?("z")
=> false
irb(main):036:0> h.key?("a")
=>true
irb(main):36:0> h.member? "b" # true

You can also use empty? to see whether there are any keys at all left in the hash; length or its alias size can be used to determine how many there are, as shown here:
irb(main):037:0> a = {"a"=>1,"b"=>2}
=> {"a"=>1, "b"=>2}
irb(main):038:0> a.empty?
=> false
irb(main):039:0> a.length
=> 2

No comments :

Post a Comment