"Luke Galea" <lgalea / gmmsolutions.com> wrote in message > curious if there is a nice ruby way to express an xpath like navigation of an > object graph.. > > XPATH way: > Countries/Provinces/Cities[ @name = "London" ] > > The only Ruby way I can think of: > countries.collect{ |c| c.provinces.collect {|p| p.cities.select { |c2| c2#name > == 'London' } } } How about : x[:countries][:provinces][:cities, {:name=>"London"}] You could build up a query by composing query objects. To evaluate the query you would have to treat each sub-query as evaluating to a result-set of tree-like things, I believe (modulo optimizations). Can probably build up a filtering proc and use #each to avoid actually constructing the result set. class Q attr_accessor :chain def initialize self.chain = [] end def [](attr=nil, hash={}) f = Q.new f.chain = self.chain.dup if attr f.chain << [attr, hash] else f.chain.last << hash end f end end require 'pp' x = Q.new pp x y=x[:a] pp y z=y[:b][:c][:d, {:foo=>"bar", :baz=>"bratz"}] pp z #==> #<Q:0x28a4668 @chain=[]> #<Q:0x28bec90 @chain=[[:a, {}]]> #<Q:0x28b8c90 @chain=[[:a, {}], [:b, {}], [:c, {}], [:d, {:foo=>"bar", :baz=>"bratz"}]]>