Class: SyntaxTree::Defs
- Inherits:
-
Object
- Object
- SyntaxTree::Defs
- Defined in:
- lib/syntax_tree.rb
Overview
Defs represents defining a singleton method on an object.
def object.method(param) result end
Instance Attribute Summary collapse
-
#bodystmt ⇒ Object
readonly
- BodyStmt
-
the expressions to be executed by the method.
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#location ⇒ Object
readonly
- Location
-
the location of this node.
-
#name ⇒ Object
readonly
- Backtick | Const | Ident | Kw | Op
-
the name of the method.
-
#operator ⇒ Object
readonly
- Op | Period
-
the operator being used to declare the method.
-
#params ⇒ Object
readonly
- Params | Paren
-
the parameter declaration for the method.
-
#target ⇒ Object
readonly
- untyped
-
the target where the method is being defined.
Instance Method Summary collapse
- #child_nodes ⇒ Object
- #format(q) ⇒ Object
-
#initialize(target:, operator:, name:, params:, bodystmt:, location:, comments: []) ⇒ Defs
constructor
A new instance of Defs.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
Constructor Details
#initialize(target:, operator:, name:, params:, bodystmt:, location:, comments: []) ⇒ Defs
Returns a new instance of Defs.
4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 |
# File 'lib/syntax_tree.rb', line 4345 def initialize( target:, operator:, name:, params:, bodystmt:, location:, comments: [] ) @target = target @operator = operator @name = name @params = params @bodystmt = bodystmt @location = location @comments = comments end |
Instance Attribute Details
#bodystmt ⇒ Object (readonly)
- BodyStmt
-
the expressions to be executed by the method
4337 4338 4339 |
# File 'lib/syntax_tree.rb', line 4337 def bodystmt @bodystmt end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
4343 4344 4345 |
# File 'lib/syntax_tree.rb', line 4343 def comments @comments end |
#location ⇒ Object (readonly)
- Location
-
the location of this node
4340 4341 4342 |
# File 'lib/syntax_tree.rb', line 4340 def location @location end |
#name ⇒ Object (readonly)
- Backtick | Const | Ident | Kw | Op
-
the name of the method
4331 4332 4333 |
# File 'lib/syntax_tree.rb', line 4331 def name @name end |
#operator ⇒ Object (readonly)
- Op | Period
-
the operator being used to declare the method
4328 4329 4330 |
# File 'lib/syntax_tree.rb', line 4328 def operator @operator end |
#params ⇒ Object (readonly)
- Params | Paren
-
the parameter declaration for the method
4334 4335 4336 |
# File 'lib/syntax_tree.rb', line 4334 def params @params end |
#target ⇒ Object (readonly)
- untyped
-
the target where the method is being defined
4325 4326 4327 |
# File 'lib/syntax_tree.rb', line 4325 def target @target end |
Instance Method Details
#child_nodes ⇒ Object
4363 4364 4365 |
# File 'lib/syntax_tree.rb', line 4363 def child_nodes [target, operator, name, params, bodystmt] end |
#format(q) ⇒ Object
4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 |
# File 'lib/syntax_tree.rb', line 4367 def format(q) q.group do q.group do q.text("def ") q.format(target) q.format(CallOperatorFormatter.new(operator)) q.format(name) if params.is_a?(Params) && !params.empty? q.text("(") q.format(params) q.text(")") else q.format(params) end end unless bodystmt.empty? q.indent do q.breakable(force: true) q.format(bodystmt) end end q.breakable(force: true) q.text("end") end end |
#pretty_print(q) ⇒ Object
4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 |
# File 'lib/syntax_tree.rb', line 4396 def pretty_print(q) q.group(2, "(", ")") do q.text("defs") q.breakable q.pp(target) q.breakable q.pp(operator) q.breakable q.pp(name) q.breakable q.pp(params) q.breakable q.pp(bodystmt) q.pp(Comment::List.new(comments)) end end |
#to_json(*opts) ⇒ Object
4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 |
# File 'lib/syntax_tree.rb', line 4419 def to_json(*opts) { type: :defs, target: target, op: operator, name: name, params: params, bodystmt: bodystmt, loc: location, cmts: comments }.to_json(*opts) end |