Method: AST::Node#updated

Defined in:
lib/ast/node.rb

#updated(type = nil, children = nil, properties = nil) ⇒ AST::Node

Returns a new instance of Node where non-nil arguments replace the corresponding fields of ‘self`.

For example, ‘Node.new(:foo, [ 1, 2 ]).updated(:bar)` would yield `(bar 1 2)`, and `Node.new(:foo, [ 1, 2 ]).updated(nil, [])` would yield `(foo)`.

If the resulting node would be identical to ‘self`, does nothing.

Parameters:

  • type (Symbol, nil) (defaults to: nil)
  • children (Array, nil) (defaults to: nil)
  • properties (Hash, nil) (defaults to: nil)

Returns:



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/ast/node.rb', line 133

def updated(type=nil, children=nil, properties=nil)
  new_type       = type       || @type
  new_children   = children   || @children
  new_properties = properties || {}

  if @type == new_type &&
      @children == new_children &&
      properties.nil?
    self
  else
    copy = original_dup
    copy.send :initialize, new_type, new_children, new_properties
    copy
  end
end