Chautauqua Logo

Ruby class Hash interactive

Prompt icon → Click to get an interactive sample (type in code)
! → Has also a bang method (modifies self)

Creating

# h = { a: 1, b: 2, c: 3 }

Methods

Creating a Hash

[]Hash[h] # {a: 1,b: 2,c: 3} Documentation Interactive samplePlayground
{a: 1, b: 2, c: 3}
newHash.new # {} Documentation Interactive samplePlayground
{}
try_convertHash.try_convert('string') # nil Documentation Interactive samplePlayground
nil

Setting Hash state

compare_by_identityh.compare_by_identity;h['a']… # {'a':1,'a':2} Documentation Interactive samplePlayground
{"a" => 1, "a" => 2}
compare_by_identity?h.compare_by_identity? # true Documentation Interactive samplePlayground
true
defaulth={};h.default=false;h.default # false Documentation Interactive samplePlayground
false
default_proch={};(h.default_proc=proc{|h|…}).class # Proc Documentation Interactive samplePlayground
Proc
rehashh={[1]=>'a'};…;h[[1, 2]];h.rehash # nil,a Documentation Interactive samplePlayground
{[1, 2] => "a"}

Querying

any?h.any?([:a, 1]; h.any?{|k,v| v<3} # true Documentation Interactive samplePlayground
true
empty?h={};h.empty? # true Documentation Interactive samplePlayground
true
eql?h1={a:1};h2={a:1};h1.eql? h2 # true Documentation Interactive samplePlayground
true
hashh.hash # 6345789 (differs) Documentation Interactive samplePlayground
value? (al… has_value?)h.value?(1) # true Documentation Interactive samplePlayground
true
key? (alias include? member? has_key?)h.key?(:a) # true Documentation Interactive samplePlayground
true
size (alias length))h.size # 3 Documentation Interactive samplePlayground
3

Comparing

<h={a:1}; h<{a:1,b:2} # true Documentation Interactive samplePlayground
true
<=h={a:1,b:2}; h<={a:1,b:2} # true Documentation Interactive samplePlayground
true
==h={a:1,b:2}; h=={a:1,b:2} # true Documentation Interactive samplePlayground
true
>h={a:1,b:2}; h>{a:1} # true Documentation Interactive samplePlayground
true
>=h={a:1,b:2}; h<={a:1,b:2} # true Documentation Interactive samplePlayground
true

Fetching

[]h[:b] # 2 Documentation Interactive samplePlayground
2
assoch.assoc(:b) # [:b,2] Documentation Interactive samplePlayground
[:b, 2]
rassoch.rassoc(2) # [:b,2] Documentation Interactive samplePlayground
[:b, 2]
digh={a:{b:{c:3}}}; h.dig(:a) # {b:{c:3}} Documentation Interactive samplePlayground
{b: {c: 3}}
fetchh.fetch(:b) # 2 Documentation Interactive samplePlayground
2
fetch_valuesh.fetch_values(:b,:c) # [2,3] Documentation Interactive samplePlayground
[2, 3]
keyh.key(2) # :b Documentation Interactive samplePlayground
:b
keysh.keys # [:a,:b,:c] Documentation Interactive samplePlayground
[:a, :b, :c]
valuesh.values # [1,2,3] Documentation Interactive samplePlayground
[1, 2, 3]
values_ath.values_at(:a,:c) # [1,3] Documentation Interactive samplePlayground
[1, 3]

Assigning

[] (alias store)h[:b]=7; h.store(:b,7) # {a:1,b:7,c:3} Documentation Interactive samplePlayground
{a: 1, b: 7, c: 3}
mergea={a:1}; b={b:2}; b.merge(a,b) # {a:1,b:2} Documentation Interactive samplePlayground
{a: 1, b: 2, c: 3}
update (alias merge!)a={a:1,b:2}; b={a:7}; a.update(b) # {a:7,…} Documentation Interactive samplePlayground
{a: 7, b: 2}
replace (alias initialize_copy)a={a:1,b:2}; a.replace(a:7,c:8) # {a:7,c:8} Documentation Interactive samplePlayground
{a: 7, c: 8}

Deleting

clearh.clear # {} Documentation Interactive samplePlayground
{}
compact!{a:1,b:nil,c:3}.compact # {a:1,c:3} Documentation Interactive samplePlayground
{a: 1, c: 3}
deleteh.delete(:b) # 2; {a:1,c:3} Documentation Interactive samplePlayground
{a: 1, c: 3}
select (alias filter)!h.select {|k,v| v<3} # {a:1mb:2} Documentation Interactive samplePlayground
{a: 1, b: 2, d: 4}
delete_ifh.delete_if {|k,v| v<3} # {c:3} Documentation Interactive samplePlayground
{c: 3, d: 4}
keep_ifh.keep_if {|k,v| v<3} # {a:1,b:2} Documentation Interactive samplePlayground
{a: 1, b: 2}
reject!h.reject {|k,v| k[0]=='b'} # {a:1,c:3} Documentation Interactive samplePlayground
{a: 1, c: 3}
shifth.shift # [:a,1]; {b:2,c:3} Documentation Interactive samplePlayground
{b: 2, c: 3}
sliceh.slice(:b,:c) # {b:2,c:3} Documentation Interactive samplePlayground
{b: 2, c: 3}
! See non-bang methods for:
compact! select!→(alias filter) reject

Iterating

each (alias each_pair)h.each {|k,v| p "#{k}-#{v}"} # a-1;b-2;c-3 Documentation Interactive samplePlayground
a-1|b-2|c-3|
each_keyh.each_key {|k| p "#{k}"} # a;b;c Documentation Interactive samplePlayground
a|b|c|
each_valueh.each_value {|v| p "#{v}"} # 1;2;3 Documentation Interactive samplePlayground
1|2|3|

Converting

flattenh3={a:1,d:[:e]}.flatten(2) # [:a,1,:d,:e] Documentation Interactive samplePlayground
[:a, {b: 1, c: 2}, :d, :e, :f, [:g]]
to_s (alias inspect)h.to_s # "[:a,1,:d,:e]" Documentation Interactive samplePlayground
"{a: 1, b: 2, c: 3}"
to_ah.to_a # [[:a,1],[:b,2],[:c,3]] Documentation Interactive samplePlayground
[[:a, 1], [:b, 2], [:c, 3]]
to_hash{a:1,b:2,:c=>3}.to_hash # {a:1,b:2,c:3} Documentation Interactive samplePlayground
{a: 1, b: 2, c: 3}
to_proch.to_proc.class # Proc Documentation Interactive samplePlayground
{a: 1, b: 2, c: 3}

Transforming Keys and Values

inverth.invert # {1 => :a,2 => :b,3 => :c} Documentation Interactive samplePlayground
{1 => :a, 2 => :b, 3 => :c}
transform_keys!h.transform_keys{|k|k.to_s} # {'a'=>1,'b'=>…} Documentation Interactive samplePlayground
{"a" => 1, "b" => 2, "c" => 3}
transform_values!h.transform_values{|v|v+5} # {a:6,b:7,c:8} Documentation Interactive samplePlayground
{a: 11, b: 12, c: 13}
! See non-bang methods for:
transform_keys! transform_values!