Class: Prism::ArrayPatternNode

Inherits:
PrismNode
  • Object
show all
Defined in:
lib/prism/node.rb,
sig/generated/prism/node.rbs,
ext/prism/api_node.c

Overview

Represents an array pattern in pattern matching.

foo in 1, 2
       ^^^^

foo in [1, 2]
       ^^^^^^

foo in *bar
       ^^^^

foo in Bar[]
       ^^^^^

foo in Bar[1, 2, 3]
       ^^^^^^^^^^^^

Explore code sample

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, constant, requireds, rest, posts, opening_loc, closing_loc) ⇒ ArrayPatternNode

Initialize a new ArrayPatternNode node.

: (Source source, Integer node_id, Location location, Integer flags, (ConstantPathNode | ConstantReadNode)? constant, Array requireds, (ImplicitRestNode | SplatNode)? rest, Array posts, Location? opening_loc, Location? closing_loc) -> void

Parameters:



1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
# File 'lib/prism/node.rb', line 1705

def initialize(source, node_id, location, flags, constant, requireds, rest, posts, opening_loc, closing_loc)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @constant = constant
  @requireds = requireds
  @rest = rest
  @posts = posts
  @opening_loc = opening_loc
  @closing_loc = closing_loc
end

Class Method Details

.type ⇒ :array_pattern_node

See Node.type.

: () -> :array_pattern_node

Returns:

  • (:array_pattern_node)


1810
1811
1812
# File 'lib/prism/node.rb', line 1810

def self.type
  :array_pattern_node
end

Instance Method Details

#===(other) ⇒ boolish

: (untyped other) -> boolish

Parameters:

  • other (Object)

Returns:

  • (boolish)


1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
# File 'lib/prism/node.rb', line 1988

def ===(other) # :nodoc:
  other.is_a?(ArrayPatternNode) &&
    (constant === other.constant) &&
    (requireds.length == other.requireds.length) &&
    requireds.zip(other.requireds).all? { |left, right| left === right } &&
    (rest === other.rest) &&
    (posts.length == other.posts.length) &&
    posts.zip(other.posts).all? { |left, right| left === right } &&
    (opening_loc.nil? == other.opening_loc.nil?) &&
    (closing_loc.nil? == other.closing_loc.nil?)
end

#accept(visitor) ⇒ Object

See Node.accept.

: (_Visitor visitor) -> untyped

Parameters:

Returns:

  • (Object)


1732
1733
1734
# File 'lib/prism/node.rb', line 1732

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

#child_nodes ⇒ Array[node?] Also known as: deconstruct

See Node.child_nodes.

: () -> Array

Returns:

  • (Array[node?])


1739
1740
1741
# File 'lib/prism/node.rb', line 1739

def child_nodes
  [constant, *requireds, rest, *posts]
end

#closing ⇒ String?

:call-seq:

closing -> String | nil

Slice the location of closing_loc from the source.

: () -> String?

Returns:

  • (String, nil)


1981
1982
1983
# File 'lib/prism/node.rb', line 1981

def closing
  closing_loc&.slice
end

#closing_loc ⇒ Location?

:category: Locations :call-seq:

closing_loc -> Location | nil

Represents the closing Location of the array pattern.

foo in [1, 2]
            ^

Explore code sample

: () -> Location?

Returns:



1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
# File 'lib/prism/node.rb', line 1943

def closing_loc
  location = @closing_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @closing_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#compact_child_nodes ⇒ Array[node]

See Node.compact_child_nodes.

: () -> Array

Returns:

  • (Array[node])


1759
1760
1761
1762
1763
1764
1765
1766
# File 'lib/prism/node.rb', line 1759

def compact_child_nodes
  compact = [] #: Array[Prism::node]
  if (constant = self.constant); compact << constant; end
  compact.concat(requireds)
  if (rest = self.rest); compact << rest; end
  compact.concat(posts)
  compact
end

#constant ⇒ ConstantPathNode, ...

:call-seq:

constant -> ConstantPathNode | ConstantReadNode | nil

Represents the optional constant preceding the Array

foo in Bar[]
       ^^^

foo in Bar[1, 2, 3]
       ^^^

foo in Bar::Baz[1, 2, 3]
       ^^^^^^^^

Explore code sample

: () -> (ConstantPathNode | ConstantReadNode)?



1840
1841
1842
# File 'lib/prism/node.rb', line 1840

def constant
  @constant
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, constant: self.constant, requireds: self.requireds, rest: self.rest, posts: self.posts, opening_loc: self.opening_loc, closing_loc: self.closing_loc) ⇒ ArrayPatternNode

:call-seq:

copy(**fields) -> ArrayPatternNode

Creates a copy of self with the given fields, using self as the template.

: (?node_id: Integer, ?location: Location, ?flags: Integer, ?constant: (ConstantPathNode | ConstantReadNode)?, ?requireds: Array, ?rest: (ImplicitRestNode | SplatNode)?, ?posts: Array, ?opening_loc: Location?, ?closing_loc: Location?) -> ArrayPatternNode

Parameters:

  • node_id: (Integer) (defaults to: self.node_id)
  • location: (Location) (defaults to: self.location)
  • flags: (Integer) (defaults to: self.flags)
  • constant: (ConstantPathNode, ConstantReadNode, nil) (defaults to: self.constant)
  • requireds: (Array[Prism::node]) (defaults to: self.requireds)
  • rest: (ImplicitRestNode, SplatNode, nil) (defaults to: self.rest)
  • posts: (Array[Prism::node]) (defaults to: self.posts)
  • opening_loc: (Location, nil) (defaults to: self.opening_loc)
  • closing_loc: (Location, nil) (defaults to: self.closing_loc)

Returns:



1774
1775
1776
# File 'lib/prism/node.rb', line 1774

def copy(node_id: self.node_id, location: self.location, flags: self.flags, constant: self.constant, requireds: self.requireds, rest: self.rest, posts: self.posts, opening_loc: self.opening_loc, closing_loc: self.closing_loc)
  ArrayPatternNode.new(source, node_id, location, flags, constant, requireds, rest, posts, opening_loc, closing_loc)
end

#deconstruct_keys(keys) ⇒ Hash[Symbol, untyped]

: (Array? keys) -> Hash[Symbol, untyped]

Parameters:

  • keys (Array[Symbol], nil)

Returns:

  • (Hash[Symbol, untyped])


1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
# File 'lib/prism/node.rb', line 1781

def deconstruct_keys(keys) # :nodoc:
  (keys || %i[node_id location constant requireds rest posts opening_loc closing_loc opening closing]).each_with_object(
    {} #: Hash[Symbol, untyped]
  ) do |key, deconstructed|
    case key
    when :node_id                 then deconstructed[:node_id] = self.node_id
    when :location                then deconstructed[:location] = self.location
    when :constant                then deconstructed[:constant] = self.constant
    when :requireds               then deconstructed[:requireds] = self.requireds
    when :rest                    then deconstructed[:rest] = self.rest
    when :posts                   then deconstructed[:posts] = self.posts
    when :opening_loc             then deconstructed[:opening_loc] = self.opening_loc
    when :closing_loc             then deconstructed[:closing_loc] = self.closing_loc
    when :opening                 then deconstructed[:opening] = self.opening
    when :closing                 then deconstructed[:closing] = self.closing
    end
  end
end

#each_child_node ⇒ Enumerator[node, void] #each_child_node ⇒ void

See Node.each_child_node.

: () -> Enumerator[node, void] : () { (node) -> void } -> void

Overloads:

  • #each_child_node ⇒ Enumerator[node, void]

    Returns:

    • (Enumerator[node, void])
  • #each_child_node ⇒ void

    This method returns an undefined value.

Yields:

Yield Parameters:

  • arg0 (node)

Yield Returns:

  • (void)


1747
1748
1749
1750
1751
1752
1753
1754
# File 'lib/prism/node.rb', line 1747

def each_child_node(&blk)
  return to_enum(:each_child_node) unless block_given?

  if (constant = self.constant); yield constant; end
  requireds.each { |node| yield node }
  if (rest = self.rest); yield rest; end
  posts.each { |node| yield node }
end

#inspect ⇒ String

: () -> String

Returns:

  • (String)


1815
1816
1817
# File 'lib/prism/node.rb', line 1815

def inspect # :nodoc:
  InspectVisitor.compose(self)
end

#opening ⇒ String?

:call-seq:

opening -> String | nil

Slice the location of opening_loc from the source.

: () -> String?

Returns:

  • (String, nil)


1971
1972
1973
# File 'lib/prism/node.rb', line 1971

def opening
  opening_loc&.slice
end

#opening_loc ⇒ Location?

:category: Locations :call-seq:

opening_loc -> Location | nil

Represents the opening Location of the array pattern.

foo in [1, 2]
       ^

Explore code sample

: () -> Location?

Returns:



1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
# File 'lib/prism/node.rb', line 1909

def opening_loc
  location = @opening_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @opening_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#posts ⇒ Array[Prism::node]

:call-seq:

posts -> Array[Node]

Represents the elements after the rest element of the array pattern.

foo in *bar, baz
             ^^^

Explore code sample

: () -> Array

Returns:

  • (Array[Prism::node])


1891
1892
1893
# File 'lib/prism/node.rb', line 1891

def posts
  @posts
end

#requireds ⇒ Array[Prism::node]

:call-seq:

requireds -> Array[Node]

Represents the required elements of the array pattern.

foo in [1, 2]
        ^  ^

Explore code sample

: () -> Array

Returns:

  • (Array[Prism::node])


1857
1858
1859
# File 'lib/prism/node.rb', line 1857

def requireds
  @requireds
end

#rest ⇒ ImplicitRestNode, ...

:call-seq:

rest -> ImplicitRestNode | SplatNode | nil

Represents the rest element of the array pattern.

foo in *bar
       ^^^^

Explore code sample

: () -> (ImplicitRestNode | SplatNode)?

Returns:



1874
1875
1876
# File 'lib/prism/node.rb', line 1874

def rest
  @rest
end

#save_closing_loc(repository) ⇒ Relocation::Entry?

:category: Repository Save the closing_loc location using the given saved source so that it can be retrieved later.

: (_Repository repository) -> Relocation::Entry?

Parameters:

Returns:



1960
1961
1962
# File 'lib/prism/node.rb', line 1960

def save_closing_loc(repository)
  repository.enter(node_id, :closing_loc) unless @closing_loc.nil?
end

#save_opening_loc(repository) ⇒ Relocation::Entry?

:category: Repository Save the opening_loc location using the given saved source so that it can be retrieved later.

: (_Repository repository) -> Relocation::Entry?

Parameters:

Returns:



1926
1927
1928
# File 'lib/prism/node.rb', line 1926

def save_opening_loc(repository)
  repository.enter(node_id, :opening_loc) unless @opening_loc.nil?
end

#type ⇒ :array_pattern_node

See Node#type.

: () -> :array_pattern_node

Returns:

  • (:array_pattern_node)


1803
1804
1805
# File 'lib/prism/node.rb', line 1803

def type
  :array_pattern_node
end