Method: YARD::Parser::Ruby::AstNode#jump

Defined in:
lib/yard/parser/ruby/ast_node.rb

#jump(*node_types) ⇒ AstNode, self

Searches through the node and all descendants and returns the first node with a type matching any of node_types, otherwise returns the original node (self).

Examples:

Returns the first method definition in a block of code

ast = YARD.parse_string("if true; def x; end end").ast
ast.jump(:def)
# => s(:def, s(:ident, "x"), s(:params, nil, nil, nil, nil,
#      nil), s(s(:void_stmt, )))

Returns first ‘def’ or ‘class’ statement

ast = YARD.parse_string("class X; def y; end end")
ast.jump(:def, :class).first
# =>

If the node types are not present in the AST

ast = YARD.parse("def x; end")
ast.jump(:def)

Parameters:

  • node_types (Array<Symbol>)

    a set of node types to match

Returns:

  • (AstNode)

    the matching node, if one was found

  • (self)

    if no node was found


191
192
193
194
# File 'lib/yard/parser/ruby/ast_node.rb', line 191

def jump(*node_types)
  traverse {|child| return(child) if node_types.include?(child.type) }
  self
end