"compilers", "color" => "blue", "sports" => "basketball", "number" =>7, "cuisine" => "mediterranean", "languages" => "Ruby", "arrays" =>[1,2,4,6,8] } puts favorites["class"]  compilers puts favorites["arrays"]  [1,2,4,6,8]"> "compilers", "color" => "blue", "sports" => "basketball", "number" =>7, "cuisine" => "mediterranean", "languages" => "Ruby", "arrays" =>[1,2,4,6,8] } puts favorites["class"]  compilers puts favorites["arrays"]  [1,2,4,6,8]">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Learning Ruby Ruby Hashes. Hashes Hashes introduce a new accessing method – similar to arrays but indexing is by arbitrary keys of any type. Not magic.

Similar presentations


Presentation on theme: "Learning Ruby Ruby Hashes. Hashes Hashes introduce a new accessing method – similar to arrays but indexing is by arbitrary keys of any type. Not magic."— Presentation transcript:

1 Learning Ruby Ruby Hashes

2 Hashes Hashes introduce a new accessing method – similar to arrays but indexing is by arbitrary keys of any type. Not magic - Implemented as hash tables (like you learned about in data structures) Unlike arrays, which associate object references with numbered indices, hashes associate object references with names/keys aka: "associative arrays", "maps" and "dictionaries” Hash entries are referred to as "key/value" or "name/value" pairs

3 Playing with hashes favorites = {"class"=> "compilers", "color" => "blue", "sports" => "basketball", "number" =>7, "cuisine" => "mediterranean", "languages" => "Ruby", "arrays" =>[1,2,4,6,8] } puts favorites["class"]  compilers puts favorites["arrays"]  [1,2,4,6,8]

4 Playing with hashes h = Hash.new("Go Fish")  “Go Fish is what to return if no value for the key exists h["a"] = 100 h["b"] = 200 puts h["a"]  100 puts h["c"]  "Go Fish"

5 Playing with hashes a= Hash["a", 100, "b", 200]  {"a"=>100, "b"=>200} b= Hash["a" => 100, "b" => 200]  {"a"=>100, "b"=>200} c= { "a" => 100, "b" => 200 }  {"a"=>100, "b"=>200} c.default = “zero”  sets default value (value returned for non-existent key – nil otherwise) c.delete(“b”)  deletes b=>200 pair c.has_key?(“dog”)  returns boolean

6 with code blocks h = { "a" => 100, "b" => 200 } h.each {|key, value| print key, " is ", value, "\n" } h = { "a" => 100, "b" => 200 } h.each_key {|key| puts key }  calls block once for each key in h, passing the key as the parameter

7 Symbols What do symbols look like? a colon followed by a non- quoted string :IamASymbol, :skinny :likeable Or a colon followed by a quoted string :’I love Ruby’ :”Vicki Allan” Symbols are immutable – they never appear on the left hand side of an assignment Simply, a symbol is something that you use to represent names and strings. What this boils down to is a way to efficiently have descriptive names while saving the space one would use to generate a string for each naming instance. Every time you use the same string, you get a new copy of it. A symbol allows them all to share the same instance. Anytime a string is used over and over, a symbol may be a good candidate for replacement.

8 The string representation is more important than the number part puts :steve puts :steve.to_s puts :steve.to_i puts :steve.class steve 1463 Symbol

9 songs = {} songs = { "July" => :Mundy,  key=> value pairs "I Predict a Riot" => :KaiserChiefs, "Rainbow" => :Mundy }  colon denotes a “symbol” having a numeric and a string value. classic_rock = { 'Smoke on the Water' => 'Deep Purple', 'Stairway to Heaven' => 'Led Zeppelin' } songs["La La La La La"] = :KaiserChiefs  Add to hash songs["Na Na Na Na Na"] = :KaiserChiefs  Add to hash Playing with Ruby Hashes

10 songs.keys songs.values songs.sort  Converts songs to a nested array of [ key, value ] arrays and sorts it by keys, using Array#sort classic_rock.each do | song, artist | puts "#{artist} performs '#{song}'." end # of do. Ruby Hash Methods

11 songs.delete( 'La La La La La' ) classic_rock.empty? classic_rock.has_key?( 'Satisfaction' ) songs.length all_songs = songs.merge( classic_rock ) hash1.merge(hash2): Returns a new hash containing the contents of hash1 and the contents of hash2, overwriting entries in hash1 with duplicate keys with those from hash2. (But may actually store the two hash tables, but use old one only when new hash doesn’t have an entry)newkeys all_songs.each {| song, artist | puts "#{artist} performs '#{song}‘ \n." } # of do. my_all_time_fav = all_songs.to_a my_all_time_fav[2] Working with Hashes

12 More... Ruby So Far Hashes can store any object reference... another hash, another array, another object... anything! This can be very, very flexible, efficient and (if you pay attention) bug-free!


Download ppt "Learning Ruby Ruby Hashes. Hashes Hashes introduce a new accessing method – similar to arrays but indexing is by arbitrary keys of any type. Not magic."

Similar presentations


Ads by Google