Class: SyntaxTree::RAssign

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

RAssign represents a single-line pattern match.

value in pattern
value => pattern

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(value:, operator:, pattern:, location:, comments: []) ⇒ RAssign

Returns a new instance of RAssign.



2725
2726
2727
2728
2729
2730
2731
# File 'lib/syntax_tree/node.rb', line 2725

def initialize(value:, operator:, pattern:, location:, comments: [])
  @value = value
  @operator = operator
  @pattern = pattern
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2723
2724
2725
# File 'lib/syntax_tree/node.rb', line 2723

def comments
  @comments
end

#operatorObject (readonly)

Kw | Op

the operator being used to match against the pattern, which is

either => or in



2717
2718
2719
# File 'lib/syntax_tree/node.rb', line 2717

def operator
  @operator
end

#patternObject (readonly)

untyped

the pattern on the right-hand side of the expression



2720
2721
2722
# File 'lib/syntax_tree/node.rb', line 2720

def pattern
  @pattern
end

#valueObject (readonly)

untyped

the left-hand expression



2713
2714
2715
# File 'lib/syntax_tree/node.rb', line 2713

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



2733
2734
2735
# File 'lib/syntax_tree/node.rb', line 2733

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

#child_nodesObject Also known as: deconstruct



2737
2738
2739
# File 'lib/syntax_tree/node.rb', line 2737

def child_nodes
  [value, operator, pattern]
end

#deconstruct_keys(_keys) ⇒ Object



2743
2744
2745
2746
2747
2748
2749
2750
2751
# File 'lib/syntax_tree/node.rb', line 2743

def deconstruct_keys(_keys)
  {
    value: value,
    operator: operator,
    pattern: pattern,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
# File 'lib/syntax_tree/node.rb', line 2753

def format(q)
  q.group do
    q.format(value)
    q.text(" ")
    q.format(operator)
    q.group do
      q.indent do
        q.breakable
        q.format(pattern)
      end
    end
  end
end