XSLT-like search in a Ruby Hash

Discussion in 'Ruby' started by Pito Salas, Aug 31, 2010.

  1. Pito Salas

    Pito Salas Guest

    I have a need for some relatively fancy searching in a hash, and I am
    wondering if there's anything that does some kind of pattern matching on
    Hash contents.

    Specifically here's an example of what I would like to be able to write:
    http://www.pastie.org/1127920

    Yes, I can write the iterators, collects, merges and what else, but
    nicer would be some kind of structured query syntax. Any ideas?

    Thanks!

    Pito
    --
    Posted via http://www.ruby-forum.com/.
     
    Pito Salas, Aug 31, 2010
    #1
    1. Advertising

  2. On 08/30/2010 05:29 PM, Pito Salas wrote:
    > I have a need for some relatively fancy searching in a hash, and I am
    > wondering if there's anything that does some kind of pattern matching on
    > Hash contents.
    >
    > Specifically here's an example of what I would like to be able to write:
    > http://www.pastie.org/1127920
    >
    > Yes, I can write the iterators, collects, merges and what else, but
    > nicer would be some kind of structured query syntax. Any ideas?
    >
    > Thanks!
    >
    > Pito


    Might be something useful here:

    http://eigenclass.org/hiki/Pattern matching over Ruby objects
     
    Joel VanderWerf, Aug 31, 2010
    #2
    1. Advertising

  3. 2010/8/31 Pito Salas <>:
    > I have a need for some relatively fancy searching in a hash, and I am
    > wondering if there's anything that does some kind of pattern matching on
    > Hash contents.
    >
    > Specifically here's an example of what I would like to be able to write:
    > http://www.pastie.org/1127920
    >
    > Yes, I can write the iterators, collects, merges and what else, but
    > nicer would be some kind of structured query syntax. Any ideas?


    For the simple case one could do

    class Hash
    def nested_value? *keys, val
    val.eql?( keys.inject(self) {|h,k| h.fetch(k) { return false } } )
    rescue return false
    end

    def nested_key? *keys
    keys.inject(self) {|h,k| h.fetch(k) { return false } } rescue return false
    true
    end
    end

    Note, you need 1.9* for this - otherwise you need to manually
    distribute arguments.

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Aug 31, 2010
    #3
    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. Luca
    Replies:
    1
    Views:
    487
    Oleg Tkachenko
    Dec 11, 2003
  2. rp
    Replies:
    1
    Views:
    537
    red floyd
    Nov 10, 2011
  3. Gavin Kistner

    Hash like JS Hash (code)

    Gavin Kistner, Feb 14, 2004, in forum: Ruby
    Replies:
    8
    Views:
    234
    Gavin Kistner
    Feb 17, 2004
  4. Pokkai Dokkai
    Replies:
    8
    Views:
    136
    Phrogz
    Nov 18, 2007
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    625
    David A. Black
    Jul 2, 2008
Loading...

Share This Page