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
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(params: nil, statements: nil, location: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(params:, statements:, location:) ⇒ Lambda
constructor
A new instance of Lambda.
Methods inherited from Node
#construct_keys, #pretty_print, #to_json
Constructor Details
#initialize(params:, statements:, location:) ⇒ Lambda
Returns a new instance of Lambda.
7025 7026 7027 7028 7029 7030 |
# File 'lib/syntax_tree/node.rb', line 7025 def initialize(params:, statements:, location:) @params = params @statements = statements @location = location @comments = [] end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
7023 7024 7025 |
# File 'lib/syntax_tree/node.rb', line 7023 def comments @comments end |
#params ⇒ Object (readonly)
- LambdaVar | Paren
-
the parameter declaration for this lambda
7017 7018 7019 |
# File 'lib/syntax_tree/node.rb', line 7017 def params @params end |
#statements ⇒ Object (readonly)
- BodyStmt | Statements
-
the expressions to be executed in this lambda
7020 7021 7022 |
# File 'lib/syntax_tree/node.rb', line 7020 def statements @statements end |
Instance Method Details
#===(other) ⇒ Object
7126 7127 7128 7129 |
# File 'lib/syntax_tree/node.rb', line 7126 def ===(other) other.is_a?(Lambda) && params === other.params && statements === other.statements end |
#accept(visitor) ⇒ Object
7032 7033 7034 |
# File 'lib/syntax_tree/node.rb', line 7032 def accept(visitor) visitor.visit_lambda(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
7036 7037 7038 |
# File 'lib/syntax_tree/node.rb', line 7036 def child_nodes [params, statements] end |
#copy(params: nil, statements: nil, location: nil) ⇒ Object
7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 |
# File 'lib/syntax_tree/node.rb', line 7040 def copy(params: nil, statements: nil, location: nil) node = Lambda.new( params: params || self.params, statements: statements || self.statements, location: location || self.location ) node.comments.concat(comments.map(&:copy)) node end |
#deconstruct_keys(_keys) ⇒ Object
7054 7055 7056 7057 7058 7059 7060 7061 |
# File 'lib/syntax_tree/node.rb', line 7054 def deconstruct_keys(_keys) { params: params, statements: statements, location: location, comments: comments } end |
#format(q) ⇒ Object
7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 |
# File 'lib/syntax_tree/node.rb', line 7063 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 |