class SyntaxTree::DefNode
Def represents defining a regular method on the current self object.
def method(param) result end def object.method(param) result end
Attributes
- nil |
Node
-
the target where the method is being defined
Public Class Methods
Source
# File lib/syntax_tree/node.rb, line 4132 def initialize(target:, operator:, name:, params:, bodystmt:, location:) @target = target @operator = operator @name = name @params = params @bodystmt = bodystmt @location = location @comments = [] end
Public Instance Methods
Source
# File lib/syntax_tree/node.rb, line 4232 def ===(other) other.is_a?(DefNode) && target === other.target && operator === other.operator && name === other.name && params === other.params && bodystmt === other.bodystmt end
Source
# File lib/syntax_tree/node.rb, line 4142 def accept(visitor) visitor.visit_def(self) end
Source
# File lib/syntax_tree/node.rb, line 4245 def arity params = self.params case params when Params params.arity when Paren params.contents.arity else 0..0 end end
Source
# File lib/syntax_tree/node.rb, line 4146 def child_nodes [target, operator, name, params, bodystmt] end
Also aliased as: deconstruct
Source
# File lib/syntax_tree/node.rb, line 4150 def copy( target: nil, operator: nil, name: nil, params: nil, bodystmt: nil, location: nil ) node = DefNode.new( target: target || self.target, operator: operator || self.operator, name: name || self.name, params: params || self.params, bodystmt: bodystmt || self.bodystmt, location: location || self.location ) node.comments.concat(comments.map(&:copy)) node end
Source
# File lib/syntax_tree/node.rb, line 4174 def deconstruct_keys(_keys) { target: target, operator: operator, name: name, params: params, bodystmt: bodystmt, location: location, comments: comments } end
Source
# File lib/syntax_tree/node.rb, line 4241 def endless? !bodystmt.is_a?(BodyStmt) end
Returns true if the method was found in the source in the “endless” form, i.e. where the method body is defined using the ‘=` operator after the method name and parameters.
Source
# File lib/syntax_tree/node.rb, line 4186 def format(q) params = self.params bodystmt = self.bodystmt q.group do q.group do q.text("def") q.text(" ") if target || name.comments.empty? if target q.format(target) q.format(CallOperatorFormatter.new(operator), stackable: false) end q.format(name) case params when Paren q.format(params) when Params q.format(params) if !params.empty? || params.comments.any? end end if endless? q.text(" =") q.group do q.indent do q.breakable_space q.format(bodystmt) end end else unless bodystmt.empty? q.indent do q.breakable_force q.format(bodystmt) end end q.breakable_force q.text("end") end end end