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
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 |
# File 'lib/syntax_tree/node.rb', line 1122 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
1120 1121 1122 |
# File 'lib/syntax_tree/node.rb', line 1120 def comments @comments end |
#constant ⇒ Object (readonly)
- nil | VarRef
-
the optional constant wrapper
1105 1106 1107 |
# File 'lib/syntax_tree/node.rb', line 1105 def constant @constant end |
#posts ⇒ Object (readonly)
- Array[ untyped ]
-
the list of positional arguments occurring after the
optional star if there is one
1117 1118 1119 |
# File 'lib/syntax_tree/node.rb', line 1117 def posts @posts end |
#requireds ⇒ Object (readonly)
- Array[ untyped ]
-
the regular positional arguments that this array
pattern is matching against
1109 1110 1111 |
# File 'lib/syntax_tree/node.rb', line 1109 def requireds @requireds end |
#rest ⇒ Object (readonly)
- nil | VarField
-
the optional starred identifier that grabs up a list of
positional arguments
1113 1114 1115 |
# File 'lib/syntax_tree/node.rb', line 1113 def rest @rest end |
Instance Method Details
#accept(visitor) ⇒ Object
1138 1139 1140 |
# File 'lib/syntax_tree/node.rb', line 1138 def accept(visitor) visitor.visit_aryptn(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
1142 1143 1144 |
# File 'lib/syntax_tree/node.rb', line 1142 def child_nodes [constant, *requireds, rest, *posts] end |
#deconstruct_keys(_keys) ⇒ Object
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 |
# File 'lib/syntax_tree/node.rb', line 1148 def deconstruct_keys(_keys) { constant: constant, requireds: requireds, rest: rest, posts: posts, location: location, comments: comments } end |
#format(q) ⇒ Object
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 |
# File 'lib/syntax_tree/node.rb', line 1159 def format(q) q.group do q.format(constant) if constant q.text("[") q.indent do q.breakable_empty parts = [*requireds] parts << RestFormatter.new(rest) if rest parts += posts q.seplist(parts) { |part| q.format(part) } end q.breakable_empty q.text("]") end end |