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.
5989 5990 5991 5992 5993 5994 |
# File 'lib/syntax_tree/node.rb', line 5989 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
5987 5988 5989 |
# File 'lib/syntax_tree/node.rb', line 5987 def comments @comments end |
#params ⇒ Object (readonly)
- LambdaVar | Paren
-
the parameter declaration for this lambda
5981 5982 5983 |
# File 'lib/syntax_tree/node.rb', line 5981 def params @params end |
#statements ⇒ Object (readonly)
- BodyStmt | Statements
-
the expressions to be executed in this lambda
5984 5985 5986 |
# File 'lib/syntax_tree/node.rb', line 5984 def statements @statements end |
Instance Method Details
#accept(visitor) ⇒ Object
5996 5997 5998 |
# File 'lib/syntax_tree/node.rb', line 5996 def accept(visitor) visitor.visit_lambda(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
6000 6001 6002 |
# File 'lib/syntax_tree/node.rb', line 6000 def child_nodes [params, statements] end |
#deconstruct_keys(_keys) ⇒ Object
6006 6007 6008 6009 6010 6011 6012 6013 |
# File 'lib/syntax_tree/node.rb', line 6006 def deconstruct_keys(_keys) { params: params, statements: statements, location: location, comments: comments } end |
#format(q) ⇒ Object
6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 |
# File 'lib/syntax_tree/node.rb', line 6015 def format(q) q.group(0, "->") 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 q.format(statements) end q.breakable end q.text("}") else q.text("do") unless statements.empty? q.indent do q.breakable q.format(statements) end end q.breakable 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 |