module Hpricot::Traverse
def nodes_at(*pos)
ele.nodes_at(1, 5, 7) # gets three nodes at offsets below the current node
ele.nodes_at(-3..-1, 1..3) # gets three nodes before and three after
This method also accepts ranges and sets of numbers.
nodes_at(1). Or, to get the previous node, use nodes_at(1).
to this node. So, for example, to get the next node, you could use
Puts together an array of neighboring nodes based on their proximity
def nodes_at(*pos) sib = parent.children i, si = 0, sib.index(self) pos.map! do |r| if r.is_a?(Range) and r.begin.is_a?(String) r = Range.new(parent.index(r.begin)-si, parent.index(r.end)-si, r.exclude_end?) end r end p pos Elements[* sib.select do |x| sel = case i - si when *pos true end i += 1 sel end ] end