Hash keys exist?

B

Benjamin Fowl

Is there a way of checking whether a group of keys exist within a hash.

For example you have the following hash

example = {:profile => { :name => "John",
:username =>
"johnny_bravo"},
:example1 => { :var1 => "test",
:var2 => "test2" }
}

now all you want is a function that can be passed an array of required
keys
eg req = [:name, :username, :var1]

this array can contain any number of required keys is not always just
keys that are in either the profile or example1 sub hashes.

I've looked at using the .keys method to compare keys but that requires
me to specify where to look in the hash.

Any help much apreciated
 
B

Benjamin Fowl

Keep this open as someone might have the answer but I have reworked the
way in which I store my hash.
 
R

Robert Klemme

Is there a way of checking whether a group of keys exist within a hash.

For example you have the following hash

example =3D {:profile =3D> { :name =3D> "John",
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0:username =3D>
"johnny_bravo"},
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 :example1 =3D> { :var1 =3D> "test= ",
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 :var2 =3D> "test2" }
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }

now all you want is a function that can be passed an array of required
keys
eg req =3D [:name, :username, :var1]

this array can contain any number of required keys is not always just
keys that are in either the profile or example1 sub hashes.

irb(main):001:0> example =3D {
irb(main):002:1* :profile =3D> { :name =3D> "John", :username =3D> "john=
ny_bravo"},
irb(main):003:1* :example1 =3D> { :var1 =3D> "test", :var2 =3D> "test2" }=
,
irb(main):004:1* }
=3D> {:profile=3D>{:name=3D>"John", :username=3D>"johnny_bravo"},
:example1=3D>{:var1=3D>"test", :var2=3D>"test2"}}

irb(main):005:0> [:name, :username, :var1].all? {|k| example.has_key? k}
=3D> false
irb(main):006:0> [:profile, :example1].all? {|k| example.has_key? k}
=3D> true

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

Is there a way of checking whether a group of keys exist within a hash.

For example you have the following hash

example = {:profile => { :name => "John",
:username =>
"johnny_bravo"},
:example1 => { :var1 => "test",
:var2 => "test2" }
}

now all you want is a function that can be passed an array of required
keys
eg req = [:name, :username, :var1]

this array can contain any number of required keys is not always just
keys that are in either the profile or example1 sub hashes.

I've looked at using the .keys method to compare keys but that requires
me to specify where to look in the hash.

Any help much apreciated

Here is a recursive solution


class Hash
def nested_keys?(*target_keys)
target_keys.all? do |target_key|
has_key?(target_key) ||
values.any? { |value| value.instance_of?(Hash) &&
value.nested_keys?(target_key)
}
end
end
end

example = {
:profile => {
:name => "John",
:username => "johnny_bravo"
},
:example1 => {
:var1 => "test",
:var2 => "test2"
},
:example2 => {
:nested! => {
:three_deep => 1
}
},
:value_is_nil => nil,
}

example.nested_keys? :profile # => true
example.nested_keys? :profilx # => false
example.nested_keys? :profile , :name # => true
example.nested_keys? :profile , :namx # => false
example.nested_keys? :profile , :name , :var1 # => true
example.nested_keys? :profile , :name , :varx # => false
example.nested_keys? :value_is_nil # => true
example.nested_keys? :three_deep # => true
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top