Class: SyntaxTree::Lambda
Overview
Lambda represents using a lambda literal (not the lambda method call).
->(value) { value * 2 }
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#params ⇒ Object
readonly
- Params | Paren
-
the parameter declaration for this lambda.
-
#statements ⇒ Object
readonly
- BodyStmt | Statements
-
the expressions to be executed in this lambda.
Attributes inherited from Node
Instance Method Summary collapse
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #deconstruct_keys(keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(params:, statements:, location:, comments: []) ⇒ Lambda
constructor
A new instance of Lambda.
Methods inherited from Node
Constructor Details
#initialize(params:, statements:, location:, comments: []) ⇒ Lambda
Returns a new instance of Lambda.
5282 5283 5284 5285 5286 5287 |
# File 'lib/syntax_tree/node.rb', line 5282 def initialize(params:, statements:, location:, comments: []) @params = params @statements = statements @location = location @comments = comments end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
5280 5281 5282 |
# File 'lib/syntax_tree/node.rb', line 5280 def comments @comments end |
#params ⇒ Object (readonly)
- Params | Paren
-
the parameter declaration for this lambda
5274 5275 5276 |
# File 'lib/syntax_tree/node.rb', line 5274 def params @params end |
#statements ⇒ Object (readonly)
- BodyStmt | Statements
-
the expressions to be executed in this lambda
5277 5278 5279 |
# File 'lib/syntax_tree/node.rb', line 5277 def statements @statements end |
Instance Method Details
#accept(visitor) ⇒ Object
5289 5290 5291 |
# File 'lib/syntax_tree/node.rb', line 5289 def accept(visitor) visitor.visit_lambda(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
5293 5294 5295 |
# File 'lib/syntax_tree/node.rb', line 5293 def child_nodes [params, statements] end |
#deconstruct_keys(keys) ⇒ Object
5299 5300 5301 5302 5303 5304 5305 5306 |
# File 'lib/syntax_tree/node.rb', line 5299 def deconstruct_keys(keys) { params: params, statements: statements, location: location, comments: comments } end |
#format(q) ⇒ Object
5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 |
# File 'lib/syntax_tree/node.rb', line 5308 def format(q) q.group(0, "->") do if params.is_a?(Paren) q.format(params) unless params.contents.empty? elsif !params.empty? q.group do q.text("(") q.format(params) q.text(")") end end q.text(" ") q.if_break do force_parens = q.parents.any? do |node| node.is_a?(Command) || node.is_a?(CommandCall) end q.text(force_parens ? "{" : "do") q.indent do q.breakable q.format(statements) end q.breakable q.text(force_parens ? "}" : "end") end.if_flat do q.text("{ ") q.format(statements) q.text(" }") end end end |