Class: YARP::HashPatternNode

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

Overview

Represents a hash pattern in pattern matching.

foo => { a: 1, b: 2 }
       ^^^^^^^^^^^^^^

foo => { a: 1, b: 2, **c }
       ^^^^^^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constant, assocs, kwrest, opening_loc, closing_loc, location) ⇒ HashPatternNode

def initialize: (constant: Node?, assocs: Array, kwrest: Node?, opening_loc: Location?, closing_loc: Location?, location: Location) -> void



3655
3656
3657
3658
3659
3660
3661
3662
# File 'lib/yarp/node.rb', line 3655

def initialize(constant, assocs, kwrest, opening_loc, closing_loc, location)
  @constant = constant
  @assocs = assocs
  @kwrest = kwrest
  @opening_loc = opening_loc
  @closing_loc = closing_loc
  @location = location
end

Instance Attribute Details

#assocsObject (readonly)

attr_reader assocs: Array



3643
3644
3645
# File 'lib/yarp/node.rb', line 3643

def assocs
  @assocs
end

#closing_locObject (readonly)

attr_reader closing_loc: Location?



3652
3653
3654
# File 'lib/yarp/node.rb', line 3652

def closing_loc
  @closing_loc
end

#constantObject (readonly)

attr_reader constant: Node?



3640
3641
3642
# File 'lib/yarp/node.rb', line 3640

def constant
  @constant
end

#kwrestObject (readonly)

attr_reader kwrest: Node?



3646
3647
3648
# File 'lib/yarp/node.rb', line 3646

def kwrest
  @kwrest
end

#opening_locObject (readonly)

attr_reader opening_loc: Location?



3649
3650
3651
# File 'lib/yarp/node.rb', line 3649

def opening_loc
  @opening_loc
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



3665
3666
3667
# File 'lib/yarp/node.rb', line 3665

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

#child_nodesObject Also known as: deconstruct

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



3670
3671
3672
# File 'lib/yarp/node.rb', line 3670

def child_nodes
  [constant, *assocs, kwrest]
end

#closingObject

def closing: () -> String?



3700
3701
3702
# File 'lib/yarp/node.rb', line 3700

def closing
  closing_loc&.slice
end

#copy(**params) ⇒ Object

def copy: (**params) -> HashPatternNode



3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
# File 'lib/yarp/node.rb', line 3675

def copy(**params)
  HashPatternNode.new(
    params.fetch(:constant) { constant },
    params.fetch(:assocs) { assocs },
    params.fetch(:kwrest) { kwrest },
    params.fetch(:opening_loc) { opening_loc },
    params.fetch(:closing_loc) { closing_loc },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

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



3690
3691
3692
# File 'lib/yarp/node.rb', line 3690

def deconstruct_keys(keys)
  { constant: constant, assocs: assocs, kwrest: kwrest, opening_loc: opening_loc, closing_loc: closing_loc, location: location }
end

#openingObject

def opening: () -> String?



3695
3696
3697
# File 'lib/yarp/node.rb', line 3695

def opening
  opening_loc&.slice
end