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
- LambdaVar | 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
#construct_keys, #pretty_print, #to_json
Constructor Details
#initialize(params:, statements:, location:, comments: []) ⇒ Lambda
Returns a new instance of Lambda.
6158 6159 6160 6161 6162 6163 |
# File 'lib/syntax_tree/node.rb', line 6158 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
6156 6157 6158 |
# File 'lib/syntax_tree/node.rb', line 6156 def comments @comments end |
#params ⇒ Object (readonly)
- LambdaVar | Paren
-
the parameter declaration for this lambda
6150 6151 6152 |
# File 'lib/syntax_tree/node.rb', line 6150 def params @params end |
#statements ⇒ Object (readonly)
- BodyStmt | Statements
-
the expressions to be executed in this lambda
6153 6154 6155 |
# File 'lib/syntax_tree/node.rb', line 6153 def statements @statements end |
Instance Method Details
#accept(visitor) ⇒ Object
6165 6166 6167 |
# File 'lib/syntax_tree/node.rb', line 6165 def accept(visitor) visitor.visit_lambda(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
6169 6170 6171 |
# File 'lib/syntax_tree/node.rb', line 6169 def child_nodes [params, statements] end |
#deconstruct_keys(_keys) ⇒ Object
6175 6176 6177 6178 6179 6180 6181 6182 |
# File 'lib/syntax_tree/node.rb', line 6175 def deconstruct_keys(_keys) { params: params, statements: statements, location: location, comments: comments } end |
#format(q) ⇒ Object
6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 |
# File 'lib/syntax_tree/node.rb', line 6184 def format(q) q.text("->") q.group do if params.is_a?(Paren) q.format(params) unless params.contents.empty? elsif params.empty? && params.comments.any? q.format(params) 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 if force_parens q.text("{") unless statements.empty? q.indent do q.breakable_space q.format(statements) end q.breakable_space end q.text("}") else q.text("do") unless statements.empty? q.indent do q.breakable_space q.format(statements) end end q.breakable_space q.text("end") end end .if_flat do q.text("{") unless statements.empty? q.text(" ") q.format(statements) q.text(" ") end q.text("}") end end end |