-
Notifications
You must be signed in to change notification settings - Fork 2
Hash
Douglas G. Allen edited this page Jan 15, 2015
·
1 revision
grades = Hash.new
grades = Hash.new(0)
grades = {"Timmy Doe" => 8}
grades.default = 0
grades = { "Jane Doe" => 10, "Jim Doe" => 6 }
grades["Dorothy Doe"] = 9
puts grades["Jane Doe"] # => 10
options = { :font_size => 10, :font_family => "Arial" }
options = { font_size: 10, font_family: "Arial" }
options[:font_size] # => 10
books = {}
books[:matz] = "The Ruby Language"
books[:black] = "The Well-Grounded Rubyist"
class Book
attr_reader :author, :title
def initialize(author, title)
@author = author
@title = title
end
def ==(other)
self.class === other and
other.author == @author and
other.title == @title
end
alias eql? ==
def hash
@author.hash ^ @title.hash # XOR
end
end
book1 = Book.new 'matz', 'Ruby in a Nutshell'
book2 = Book.new 'matz', 'Ruby in a Nutshell'
reviews = {}
reviews[book1] = 'Great reference!'
reviews[book2] = 'Nice and compact!'
reviews.length #=> 1
puts Hash["a", 100, "b", 200].inspect #=> {"a"=>100, "b"=>200}
puts Hash[ [ ["a", 100], ["b", 200] ] ].inspect #=> {"a"=>100, "b"=>200}
puts Hash["a" => 100, "b" => 200].inspect #=> {"a"=>100, "b"=>200}
h = Hash.new("Go Fish")
h["a"] = 100
h["b"] = 200
puts h["a"].inspect #=> 100
puts h["c"].inspect #=> "Go Fish"
puts h["c"].upcase!.inspect #=> "GO FISH"
puts h["d"].inspect #=> "GO FISH"
puts h.keys.inspect #=> ["a", "b"]
h = Hash.new { |hash, key| hash[key] = "Go Fish: #{key}" }
puts h["c"].inspect #=> "Go Fish: c"
puts h["c"].upcase!.inspect #=> "GO FISH: C"
puts h["d"].inspect #=> "Go Fish: d"
puts h.keys.inspect #=> ["c", "d"]
puts Hash.try_convert({1=>2}).inspect # => {1=>2}
puts Hash.try_convert("1=>2").inspect # => nil
h1 = { "a" => 1, "c" => 2 }
h2 = { 7 => 35, "c" => 2, "a" => 1 }
h3 = { "a" => 1, "c" => 2, 7 => 35 }
h4 = { "a" => 1, "d" => 2, "f" => 35 }
puts (h1 == h2).inspect #=> false
puts (h2 == h3).inspect #=> true
puts (h3 == h4).inspect #=> false
h = { "a" => 100, "b" => 200 }
puts h["a"] #=> 100
puts h["c"] #=> nil
h = { "a" => 100, "b" => 200 }
h["a"] = 9
h["c"] = 4
puts h.inspect #=> {"a"=>9, "b"=>200, "c"=>4}
puts h.store("d", 42).inspect #=> {"a"=>9, "b"=>200, "c"=>4, "d"=>42}
a = "a"
b = "b".freeze
h = { a => 100, b => 200 }
puts (h.key(100).equal? a).inspect #=> false
puts (h.key(200).equal? b).inspect #=> true
h = {"colors" => ["red", "blue", "green"], "letters" => ["a", "b", "c" ]}
puts h.assoc("letters").inspect #=> ["letters", ["a", "b", "c"]]
puts h.assoc("foo").inspect #=> nil
h = { "a" => 100, "b" => 200 }
puts h.inspect #=> {"a"=>100, "b"=>200}
puts h.clear.inspect #=> {}
h1 = { "a" => 100, "b" => 200, :c => "c" }
puts h1["a"].inspect #=> 100
puts h1.compare_by_identity
puts h1.compare_by_identity? #=> true
puts h1["a"].inspect #=> nil # different objects.
puts h1[:c].inspect #=> "c" # same symbols are all same.
h = Hash.new
puts h.inspect #=> {}
puts h.default.inspect #=> nil
puts h.default(2).inspect #=> nil
h = Hash.new("cat")
puts h.inspect #=> {}
puts h.default.inspect #=> "cat"
puts h.default(2).inspect #=> "cat"
h = Hash.new {|h,k| h[k] = k.to_i*10}
puts h.inspect #=> {}
puts h.default.inspect #=> nil
puts h.default(2).inspect #=> 20
h = { "a" => 100, "b" => 200 }
h.default = "Go fish"
puts h["a"].inspect #=> 100
puts h["z"].inspect #=> "Go fish"
h = Hash.new {|h,k| h[k] = k*k }
puts h.inspect #=> {}
p = h.default_proc
a = []
puts a.inspect #=> []
p.call(a, 2)
puts a.inspect #=> [nil, nil, 4]
h.default_proc = proc do |hash, key|
hash[key] = key + key
end
puts h[2].inspect #=> 4
puts h["cat"].inspect #=> "catcat"
h = { "a" => 100, "b" => 200 }
puts h.delete("a").inspect #=> 100
puts h.delete("z").inspect #=> nil
puts h.delete("z") { |el| "#{el} not found" }.inspect #=> "z not found"
h = { "a" => 100, "b" => 200, "c" => 300 }
puts h.delete_if {|key, value| key >= "b" }.inspect #=> {"a"=>100}
h.each {|key, value| puts "#{key} is #{value}" }
h.each_key {|key| puts key }
h.each_pair {|key, value| puts "#{key} is #{value}" }
h.each_value {|value| puts value }
puts {}.empty? #=> true
puts h.fetch("a").inspect #=> 100
puts h.fetch("z", "go fish").inspect #=> "go fish"
puts h.fetch("z") { |el| "go fish, #{el}"}.inspect #=> "go fish, z"
# puts h.fetch("z").inspect
a = {1=> "one", 2 => [2,"two"], 3 => "three"}
puts a.flatten.inspect # => [1, "one", 2, [2, "two"], 3, "three"]
puts a.flatten(2).inspect # => [1, "one", 2, 2, "two", 3, "three"]
puts h.has_key?("a").inspect #=> true
puts h.has_key?("z").inspect #=> false
puts h.has_value?(100).inspect #=> true
puts h.has_value?(999).inspect #=> false
puts h.include?("a").inspect #=> true
puts h.include?("z").inspect #=> false
h = { "c" => 300, "a" => 100, "d" => 400, "c" => 300 }
puts h.inspect #=> "{\"c\"=>300, \"a\"=>100, \"d\"=>400}"
h = { "n" => 100, "m" => 100, "y" => 300, "d" => 200, "a" => 0 }
puts h.invert.inspect #=> {0=>"a", 100=>"m", 200=>"d", 300=>"y"}
h = { "a" => 100, "b" => 200, "c" => 300, "d" => 300 }
puts h.key(200).inspect #=> "b"
puts h.key(300).inspect #=> "c"
puts h.key(999).inspect #=> nil
puts h.key?("a").inspect #=> true
puts h.key?("z").inspect #=> false
puts h.keys.inspect #=> ["a", "b", "c", "d"]
puts h.length.inspect #=> 4
puts h.delete("a").inspect #=> 200
puts h.length.inspect #=> 3
puts h.member?("a").inspect #=> false
puts h.member?("z").inspect #=> false
h1 = { "a" => 100, "b" => 200 }
h2 = { "b" => 254, "c" => 300 }
puts h1.merge(h2).inspect #=> {"a"=>100, "b"=>254, "c"=>300}
puts h1.merge(h2){|key, oldval, newval| newval - oldval}.inspect #=> {"a"=>100, "b"=>54, "c"=>300}
puts h1.inspect #=> {"a"=>100, "b"=>200}
h1 = { "a" => 100, "b" => 200 }
h2 = { "b" => 254, "c" => 300 }
puts h1.merge!(h2).inspect #=> {"a"=>100, "b"=>254, "c"=>300}
h1 = { "a" => 100, "b" => 200 }
h2 = { "b" => 254, "c" => 300 }
puts h1.merge!(h2) { |key, v1, v2| v1 }.inspect #=> {"a"=>100, "b"=>200, "c"=>300}
a = {1=> "one", 2 => "two", 3 => "three", "ii" => "two"}
puts a.rassoc("two").inspect #=> [2, "two"]
puts a.rassoc("four").inspect #=> nil
a = [ "a", "b" ]
c = [ "c", "d" ]
h = { a => 100, c => 300 }
puts h[a].inspect #=> 100
a[0] = "z"
puts h[a].inspect #=> nil
puts h.rehash.inspect #=> {["z", "b"]=>100, ["c", "d"]=>300}
puts h[a].inspect #=> 100
h = { "a" => 100, "b" => 200, "c" => 300 }
puts h.reject {|k,v| k < "b"}.inspect #=> {"b" => 200, "c" => 300}
puts h.reject {|k,v| v > 100}.inspect #=> {"a" => 100}
puts h.replace({ "c" => 300, "d" => 400 }).inspect #=> {"c"=>300, "d"=>400}
h = { "a" => 100, "b" => 200, "c" => 300 }
puts h.select {|k,v| k > "a"}.inspect #=> {"b" => 200, "c" => 300}
puts h.select {|k,v| v < 200}.inspect #=> {"a" => 100}
h = { 1 => "a", 2 => "b", 3 => "c" }
puts h.shift.inspect #=> [1, "a"]
puts h.inspect #=> {2=>"b", 3=>"c"}
h = { "d" => 100, "a" => 200, "v" => 300, "e" => 400 }
puts h.length.inspect #=> 4
puts h.delete("a").inspect #=> 200
puts h.length.inspect #=> 3
h = { "a" => 100, "b" => 200 }
h["a"] = 9
h["c"] = 4
puts h.inspect #=> {"a"=>9, "b"=>200, "c"=>4}
puts h.store("d", 42).inspect #=> {"a"=>9, "b"=>200, "c"=>4, "d"=>42}
a = "a"
b = "b".freeze
h = { a => 100, b => 200 }
puts (h.key(100).equal? a).inspect #=> false
puts (h.key(200).equal? b).inspect #=> true
h = { "c" => 300, "a" => 100, "d" => 400, "c" => 300 }
puts h.to_a.inspect #=> [["c", 300], ["a", 100], ["d", 400]]
h = { "c" => 300, "a" => 100, "d" => 400, "c" => 300 }
puts h.to_s #=> {"c"=>300, "a"=>100, "d"=>400}
h1 = { "a" => 100, "b" => 200 }
h2 = { "b" => 254, "c" => 300 }
puts h1.update(h2).inspect #=> {"a"=>100, "b"=>254, "c"=>300}
h1 = { "a" => 100, "b" => 200 }
h2 = { "b" => 254, "c" => 300 }
puts h1.update(h2) { |key, v1, v2| v1 }.inspect #=> {"a"=>100, "b"=>200, "c"=>300}
puts h.value?(100).inspect #=> true
puts h.value?(999).inspect #=> false
puts h.values.inspect #=> [100, 200, 300]
h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" }
puts h.values_at("cow", "cat").inspect #=> ["bovine", "feline"]