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
#construct_keys, #pretty_print, #to_json
Constructor Details
#initialize(params:, statements:, location:, comments: []) ⇒ Lambda
Returns a new instance of Lambda.
5889 5890 5891 5892 5893 5894 |
# File 'lib/syntax_tree/node.rb', line 5889 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
5887 5888 5889 |
# File 'lib/syntax_tree/node.rb', line 5887 def comments @comments end |
#params ⇒ Object (readonly)
- Params | Paren
-
the parameter declaration for this lambda
5881 5882 5883 |
# File 'lib/syntax_tree/node.rb', line 5881 def params @params end |
#statements ⇒ Object (readonly)
- BodyStmt | Statements
-
the expressions to be executed in this lambda
5884 5885 5886 |
# File 'lib/syntax_tree/node.rb', line 5884 def statements @statements end |
Instance Method Details
#accept(visitor) ⇒ Object
5896 5897 5898 |
# File 'lib/syntax_tree/node.rb', line 5896 def accept(visitor) visitor.visit_lambda(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
5900 5901 5902 |
# File 'lib/syntax_tree/node.rb', line 5900 def child_nodes [params, statements] end |
#deconstruct_keys(_keys) ⇒ Object
5906 5907 5908 5909 5910 5911 5912 5913 |
# File 'lib/syntax_tree/node.rb', line 5906 def deconstruct_keys(_keys) { params: params, statements: statements, location: location, comments: comments } end |
#format(q) ⇒ Object
5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 |
# File 'lib/syntax_tree/node.rb', line 5915 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 |