Class: SyntaxTree::In
Overview
In represents using the in keyword within the Ruby 2.7+ pattern matching syntax.
case value
in pattern
end
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#consequent ⇒ Object
readonly
- nil | In | Else
-
the next clause in the chain.
-
#pattern ⇒ Object
readonly
- untyped
-
the pattern to check against.
-
#statements ⇒ Object
readonly
- Statements
-
the expressions to execute if the pattern matched.
Attributes inherited from Node
Instance Method Summary collapse
- #child_nodes ⇒ Object (also: #deconstruct)
- #deconstruct_keys(keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(pattern:, statements:, consequent:, location:, comments: []) ⇒ In
constructor
A new instance of In.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
Constructor Details
#initialize(pattern:, statements:, consequent:, location:, comments: []) ⇒ In
Returns a new instance of In.
6231 6232 6233 6234 6235 6236 6237 |
# File 'lib/syntax_tree/node.rb', line 6231 def initialize(pattern:, statements:, consequent:, location:, comments: []) @pattern = pattern @statements = statements @consequent = consequent @location = location @comments = comments end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
6229 6230 6231 |
# File 'lib/syntax_tree/node.rb', line 6229 def comments @comments end |
#consequent ⇒ Object (readonly)
- nil | In | Else
-
the next clause in the chain
6226 6227 6228 |
# File 'lib/syntax_tree/node.rb', line 6226 def consequent @consequent end |
#pattern ⇒ Object (readonly)
- untyped
-
the pattern to check against
6220 6221 6222 |
# File 'lib/syntax_tree/node.rb', line 6220 def pattern @pattern end |
#statements ⇒ Object (readonly)
- Statements
-
the expressions to execute if the pattern matched
6223 6224 6225 |
# File 'lib/syntax_tree/node.rb', line 6223 def statements @statements end |
Instance Method Details
#child_nodes ⇒ Object Also known as: deconstruct
6239 6240 6241 |
# File 'lib/syntax_tree/node.rb', line 6239 def child_nodes [pattern, statements, consequent] end |
#deconstruct_keys(keys) ⇒ Object
6245 6246 6247 6248 6249 6250 6251 6252 6253 |
# File 'lib/syntax_tree/node.rb', line 6245 def deconstruct_keys(keys) { pattern: pattern, statements: statements, consequent: consequent, location: location, comments: comments } end |
#format(q) ⇒ Object
6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 |
# File 'lib/syntax_tree/node.rb', line 6255 def format(q) keyword = "in " q.group do q.text(keyword) q.nest(keyword.length) { q.format(pattern) } unless statements.empty? q.indent do q.breakable(force: true) q.format(statements) end end if consequent q.breakable(force: true) q.format(consequent) end end end |
#pretty_print(q) ⇒ Object
6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 |
# File 'lib/syntax_tree/node.rb', line 6276 def pretty_print(q) q.group(2, "(", ")") do q.text("in") q.breakable q.pp(pattern) q.breakable q.pp(statements) if consequent q.breakable q.pp(consequent) end q.pp(Comment::List.new(comments)) end end |
#to_json(*opts) ⇒ Object
6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 |
# File 'lib/syntax_tree/node.rb', line 6295 def to_json(*opts) { type: :in, pattern: pattern, stmts: statements, cons: consequent, loc: location, cmts: comments }.to_json(*opts) end |