XSLT-like search in a Ruby Hash

P

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?

Thanks!

Pito
 
J

Joel VanderWerf

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
 
R

Robert Klemme

2010/8/31 Pito Salas said:
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
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top