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

#pretty_print, #to_json

Constructor Details

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

Returns a new instance of RAssign.



2324
2325
2326
2327
2328
2329
2330
# File 'lib/syntax_tree/node.rb', line 2324

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



2322
2323
2324
# File 'lib/syntax_tree/node.rb', line 2322

def comments
  @comments
end

#operatorObject (readonly)

Kw | Op

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

either => or in



2316
2317
2318
# File 'lib/syntax_tree/node.rb', line 2316

def operator
  @operator
end

#patternObject (readonly)

untyped

the pattern on the right-hand side of the expression



2319
2320
2321
# File 'lib/syntax_tree/node.rb', line 2319

def pattern
  @pattern
end

#valueObject (readonly)

untyped

the left-hand expression



2312
2313
2314
# File 'lib/syntax_tree/node.rb', line 2312

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



2332
2333
2334
# File 'lib/syntax_tree/node.rb', line 2332

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

#child_nodesObject Also known as: deconstruct



2336
2337
2338
# File 'lib/syntax_tree/node.rb', line 2336

def child_nodes
  [value, operator, pattern]
end

#deconstruct_keys(keys) ⇒ Object



2342
2343
2344
2345
2346
2347
2348
2349
2350
# File 'lib/syntax_tree/node.rb', line 2342

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

#format(q) ⇒ Object



2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
# File 'lib/syntax_tree/node.rb', line 2352

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