>> h = {:one => 1, :two => 2}
=> {:one=>1, :two=>2}
>> h.index 1
=>
>> h.index 2
=>
>> h.key?(:a)
=>
>> h.key?(:one)
=>
>> h.has_key?(:one)
=>
>> h.include?(:one)
=>
>> h.member?(:one)
=>
>> h.value?(1)
=>
>> h.value?(3)
=>
>> h.has_value?(1)
=>
default retorna el valor por defecto.
¿Que resultados se obtienen en las siguientes operaciones?
>> h = Hash.new("Go Fish")
=> {}
>> h.default
=>
>> h["a"] = 100
=> 100
>> h["b"] = 200
=>
>> h["a"]
=>
>> h["c"]
=>
>> h["c"].upcase!
=> "GO FISH"
>> h["c"]
=>
>> h["d"]
=>
>> h.keys
=>
Hash
un bloque de código
que se encarga de computar el valor por defecto.
Al bloque se le pasa el hash y la clave.
¿Que resultados se obtienen en las siguientes operaciones?
>> h = Hash.new { |hash, key| hash[key] = "Go Fish: #{key}" }
=> {}
>> h["c"]
=>
>> h["d"]
=>
>> h.keys
=>
¿Que resultados se obtienen en las siguientes operaciones?
>> fact = Hash.new { |h,k| if k > 1 then h[k] = k*h[k-1] else h[1] = 1 end }
=> {}
>> fact[4]
=>
>> fact
=>
>> fib = Hash.new { |h,k| if k > 1 then h[k] = h[k-1]+h[k-2] end }
=> {}
>> fib[0], fib[1] = 0, 1
=> [0, 1]
>> fib[7]
=> 13
>> fib
=>
>> fib.sort
=>
>> fib[12]
=> 144
>> fib.sort
=>
>> fib.keys.sort { |a,b| a.to_s <=> b.to_s }.each { |k| puts "#{k} => #{fib[k]}" }
=>
>> h = {}
=> {}
>> h[:a] = 1
=> 1
>> h.store(:b, 2)
=>
>> h
=>
>> h.replace({1 => :a, 2 => :b})
=> {1=>:a, 2=>:b}
>> h
=>
>> h.replace({1 => :a, 2 => :b, 3 => :c})
=>
>>
?> class Hash
>> def []= (*keys)
>> value = keys.pop
>> keys.each{|key| store(key, value) }
>> end
>> end
=> nil
>> hsh = {}
=> {}
>> hsh[:a, :b, :c] = 42
=> 42
>> hsh
=>
>> hsh[:a, :b] = "foo"
=> "foo"
>> hsh[:a].upcase!
=>
>> hsh[:b]
=>
>> h = { :a => 1, :b => 2 }
=> {:b=>2, :a=>1}
>> h.fetch(:a)
=> 1
>> h.fetch(:c)
IndexError: key not found
from (irb):18:in `fetch'
from (irb):18
from :0
>> h[:c]
=>
>> h.fetch(:c, 33)
=>
>> h
=> {:b=>2, :a=>1}
>> h.fetch(:c) { |k| "#{k} oppps!" }
=>
>> h = { :a => 1, :b => 2, :c => 3 }
=> {:b=>2, :c=>3, :a=>1}
>> h.values_at(:a, :b)
=> [1, 2]
>> h.values_at(:d, :d, :a)
=>
>> h.values_at(:c)
=>
>> h = { :a => 1, :b => 2, :c => 3 }
=> {:b=>2, :c=>3, :a=>1}
>> h.select { |k,v| v % 2 == 0 }
=>
>> h = { :a => 1, :b => 2}
=> {:b=>2, :a=>1}
>> h.merge({:a => 3, :c => 3})
=>
>> h
=>
>> h.merge!({:a => 3, :c => 3})
=>
>> h
=>
>> h = { :a => 1, :b => 2}
=> {:b=>2, :a=>1}
>> j = {:a => 3, :c => 3}
=> {:c=>3, :a=>3}
>> h.merge(j) { |k,a,b| a}
=>
>> h.merge(j) { |k,a,b| (a+b)/2}
=>
Hash dispone del método Hash[ [key =>|, value]* ]
que crea un nuevo hash con los objetos específicados.
Es equivalente a { key, value, ... }.
Debe haber un número par de argumentos.
¿Que resultados se obtienen en las siguientes operaciones?
>> a = [:a, 1, :b, 2, :c, 3]
=> [:a, 1, :b, 2, :c, 3]
>> Hash[*a]
=>
>> b = [[:a, 1], [:b, 2], [:c, 3]]
=> [[:a, 1], [:b, 2], [:c, 3]]
>> b.flatten
=>
>> Hash[*b.flatten]
=>
>> x = ["a" => 100, "b" => 200]
=> [{"a"=>100, "b"=>200}]
>> h = Hash[*x]
=>
>> h = { :a => 1, :b => 2}
=> {:b=>2, :a=>1}
>> Array[*h]
=>
>> Array[*h].flatten
=>
>> a = ('a'..'z').to_a
=>
>> a.map { |x| [ x, nil ] }
=>
>> a.map { |x| [ x, nil ] }.flatten
=>
>> Hash[* a.map { |x| [ x, nil ] }.flatten]
=>
>> h = {:a => 1, :b =>2, :c => 3}
=> {:b=>2, :c=>3, :a=>1}
>> h.invert
=>
>> h.to_s
=>
>> h.inspect
=>
Casiano Rodriguez León 2015-01-07