1 / 12

Learning Ruby

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 - Implemented as hash tables (like you learned about in data structures)

elia
Download Presentation

Learning Ruby

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 steve 1463 Symbol

  9. Playing with Ruby Hashes 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 LaLaLa"] = :KaiserChiefs Add to hash songs["Na Na NaNaNa"] = :KaiserChiefs Add to hash

  10. Ruby Hash Methods 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.

  11. Working with Hashes songs.delete( 'La La LaLaLa' ) 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) 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]

  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!

More Related