Hash keys exist?

Discussion in 'Ruby' started by Benjamin Fowl, Jan 18, 2011.

  1. 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

    --
    Posted via http://www.ruby-forum.com/.
     
    Benjamin Fowl, Jan 18, 2011
    #1
    1. Advertising

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

    --
    Posted via http://www.ruby-forum.com/.
     
    Benjamin Fowl, Jan 18, 2011
    #2
    1. Advertising

  3. On Tue, Jan 18, 2011 at 11:05 AM, Benjamin Fowl <> wrote:
    > 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/
     
    Robert Klemme, Jan 18, 2011
    #3
  4. Benjamin Fowl

    Josh Cheek Guest

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

    On Tue, Jan 18, 2011 at 4:05 AM, Benjamin Fowl <> wrote:

    > 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
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >


    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
     
    Josh Cheek, Jan 18, 2011
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    594
    red floyd
    Nov 10, 2011
  2. Xeno Campanoli
    Replies:
    16
    Views:
    288
    Martin DeMello
    Aug 25, 2005
  3. Alex Fenton

    Hash#values and Hash#keys order

    Alex Fenton, Apr 7, 2006, in forum: Ruby
    Replies:
    1
    Views:
    162
    George Ogata
    Apr 15, 2006
  4. Mage

    hash.keys and hash.values

    Mage, Aug 13, 2006, in forum: Ruby
    Replies:
    14
    Views:
    197
  5. Ronald Fischer

    Hash#keys, Hash#values order question

    Ronald Fischer, Aug 23, 2007, in forum: Ruby
    Replies:
    0
    Views:
    172
    Ronald Fischer
    Aug 23, 2007
Loading...

Share This Page