Class: YARP::ConstantOrWriteNode

Inherits:
YARPNode
  • Object
show all
Defined in:
lib/yarp/node.rb,
ext/yarp/api_node.c

Overview

Represents the use of the ‘||=` operator for assignment to a constant.

Target ||= value
^^^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, name_loc, operator_loc, value, location) ⇒ ConstantOrWriteNode

def initialize: (name: Symbol, name_loc: Location, operator_loc: Location, value: Node, location: Location) -> void



2717
2718
2719
2720
2721
2722
2723
# File 'lib/yarp/node.rb', line 2717

def initialize(name, name_loc, operator_loc, value, location)
  @name = name
  @name_loc = name_loc
  @operator_loc = operator_loc
  @value = value
  @location = location
end

Instance Attribute Details

#nameObject (readonly)

attr_reader name: Symbol



2705
2706
2707
# File 'lib/yarp/node.rb', line 2705

def name
  @name
end

#name_locObject (readonly)

attr_reader name_loc: Location



2708
2709
2710
# File 'lib/yarp/node.rb', line 2708

def name_loc
  @name_loc
end

#operator_locObject (readonly)

attr_reader operator_loc: Location



2711
2712
2713
# File 'lib/yarp/node.rb', line 2711

def operator_loc
  @operator_loc
end

#valueObject (readonly)

attr_reader value: Node



2714
2715
2716
# File 'lib/yarp/node.rb', line 2714

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



2726
2727
2728
# File 'lib/yarp/node.rb', line 2726

def accept(visitor)
  visitor.visit_constant_or_write_node(self)
end

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array[nil | Node]



2731
2732
2733
# File 'lib/yarp/node.rb', line 2731

def child_nodes
  [value]
end

#comment_targetsObject

def comment_targets: () -> Array[Node | Location]



2736
2737
2738
# File 'lib/yarp/node.rb', line 2736

def comment_targets
  [name_loc, operator_loc, value]
end

#copy(**params) ⇒ Object

def copy: (**params) -> ConstantOrWriteNode



2741
2742
2743
2744
2745
2746
2747
2748
2749
# File 'lib/yarp/node.rb', line 2741

def copy(**params)
  ConstantOrWriteNode.new(
    params.fetch(:name) { name },
    params.fetch(:name_loc) { name_loc },
    params.fetch(:operator_loc) { operator_loc },
    params.fetch(:value) { value },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (keys: Array) -> Hash[Symbol, nil | Node | Array | String | Token | Array | Location]



2755
2756
2757
# File 'lib/yarp/node.rb', line 2755

def deconstruct_keys(keys)
  { name: name, name_loc: name_loc, operator_loc: operator_loc, value: value, location: location }
end

#inspect(inspector = NodeInspector.new) ⇒ Object



2764
2765
2766
2767
2768
2769
2770
2771
2772
# File 'lib/yarp/node.rb', line 2764

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  inspector << "├── name: #{name.inspect}\n"
  inspector << "├── name_loc: #{inspector.location(name_loc)}\n"
  inspector << "├── operator_loc: #{inspector.location(operator_loc)}\n"
  inspector << "└── value:\n"
  inspector << inspector.child_node(value, "    ")
  inspector.to_str
end

#operatorObject

def operator: () -> String



2760
2761
2762
# File 'lib/yarp/node.rb', line 2760

def operator
  operator_loc.slice
end