# File lib/wordnet/synset.rb, line 659 def traverse( type, includeOrigin=true ) raise ArgumentError, "Illegal parameter 1: Must be either a String or a Symbol" unless type.kind_of?( String ) || type.kind_of?( Symbol ) raise ArgumentError, "Synset doesn't support the #{type.to_s} pointer type." unless self.respond_to?( type ) foundSyns = [] depth = 0 traversalFunc = nil # Build a traversal function which we can call recursively. It'll return # the synsets it traverses. traversalFunc = Proc.new {|syn,newDepth| # Flag to continue traversal haltFlag = false # Call the block if it exists and we're either past the origin or # including it if block_given? && (newDepth > 0 || includeOrigin) res = yield( syn, newDepth ) haltFlag = true if res.is_a? TrueClass end # Make an array for holding sub-synsets we see subSyns = [] subSyns.push( syn ) unless newDepth == 0 && !includeOrigin # Iterate over each synset returned by calling the pointer on the # current syn. For each one, we call ourselves recursively, and # break out of the iterator with a false value if the block has # indicated we should abort by returning a false value. unless haltFlag syn.send( type ).each {|subSyn| subSubSyns, haltFlag = traversalFunc.call( subSyn, newDepth + 1 ) subSyns.push( *subSubSyns ) unless subSubSyns.empty? break if haltFlag } end # return [ subSyns, haltFlag ] } # Call the iterator traversedSets, haltFlag = traversalFunc.call( self, depth ) # If a block was given, just return whether or not the block was halted. if block_given? return haltFlag # If no block was given, return the traversed synsets else return traversedSets end end