Class: SyntaxTree::AryPtn
Overview
AryPtn represents matching against an array pattern using the Ruby 2.7+ pattern matching syntax. It’s one of the more complicated nodes, because the four parameters that it accepts can almost all be nil.
case [1, 2, 3]
in [Integer, Integer]
"matched"
in Container[Integer, Integer]
"matched"
in [Integer, *, Integer]
"matched"
end
An AryPtn node is created with four parameters: an optional constant wrapper, an array of positional matches, an optional splat with identifier, and an optional array of positional matches that occur after the splat. All of the in clauses above would create an AryPtn node.
Defined Under Namespace
Classes: RestFormatter
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#constant ⇒ Object
readonly
- nil | VarRef
-
the optional constant wrapper.
-
#posts ⇒ Object
readonly
- Array[ untyped ]
-
the list of positional arguments occurring after the optional star if there is one.
-
#requireds ⇒ Object
readonly
- Array[ untyped ]
-
the regular positional arguments that this array pattern is matching against.
-
#rest ⇒ Object
readonly
- nil | VarField
-
the optional starred identifier that grabs up a list of positional arguments.
Attributes inherited from Node
Instance Method Summary collapse
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(constant:, requireds:, rest:, posts:, location:, comments: []) ⇒ AryPtn
constructor
A new instance of AryPtn.
Methods inherited from Node
#construct_keys, #pretty_print, #to_json
Constructor Details
#initialize(constant:, requireds:, rest:, posts:, location:, comments: []) ⇒ AryPtn
Returns a new instance of AryPtn.
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 |
# File 'lib/syntax_tree/node.rb', line 1066 def initialize( constant:, requireds:, rest:, posts:, location:, comments: [] ) @constant = constant @requireds = requireds @rest = rest @posts = posts @location = location @comments = comments end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
1064 1065 1066 |
# File 'lib/syntax_tree/node.rb', line 1064 def comments @comments end |
#constant ⇒ Object (readonly)
- nil | VarRef
-
the optional constant wrapper
1049 1050 1051 |
# File 'lib/syntax_tree/node.rb', line 1049 def constant @constant end |
#posts ⇒ Object (readonly)
- Array[ untyped ]
-
the list of positional arguments occurring after the
optional star if there is one
1061 1062 1063 |
# File 'lib/syntax_tree/node.rb', line 1061 def posts @posts end |
#requireds ⇒ Object (readonly)
- Array[ untyped ]
-
the regular positional arguments that this array
pattern is matching against
1053 1054 1055 |
# File 'lib/syntax_tree/node.rb', line 1053 def requireds @requireds end |
#rest ⇒ Object (readonly)
- nil | VarField
-
the optional starred identifier that grabs up a list of
positional arguments
1057 1058 1059 |
# File 'lib/syntax_tree/node.rb', line 1057 def rest @rest end |
Instance Method Details
#accept(visitor) ⇒ Object
1082 1083 1084 |
# File 'lib/syntax_tree/node.rb', line 1082 def accept(visitor) visitor.visit_aryptn(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
1086 1087 1088 |
# File 'lib/syntax_tree/node.rb', line 1086 def child_nodes [constant, *requireds, rest, *posts] end |
#deconstruct_keys(_keys) ⇒ Object
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 |
# File 'lib/syntax_tree/node.rb', line 1092 def deconstruct_keys(_keys) { constant: constant, requireds: requireds, rest: rest, posts: posts, location: location, comments: comments } end |
#format(q) ⇒ Object
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 |
# File 'lib/syntax_tree/node.rb', line 1103 def format(q) parts = [*requireds] parts << RestFormatter.new(rest) if rest parts += posts if constant q.group do q.format(constant) q.text("[") q.seplist(parts) { |part| q.format(part) } q.text("]") end return end parent = q.parent if parts.length == 1 || PATTERNS.include?(parent.class) q.text("[") q.seplist(parts) { |part| q.format(part) } q.text("]") elsif parts.empty? q.text("[]") else q.group { q.seplist(parts) { |part| q.format(part) } } end end |