Class: Prism::LambdaNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::LambdaNode
- Defined in:
- lib/prism/node.rb,
ext/prism/api_node.c
Overview
Represents using a lambda literal (not the lambda method call).
->(value) { value * 2 }
^^^^^^^^^^^^^^^^^^^^^^^
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
attr_reader body: Node?.
-
#closing_loc ⇒ Object
readonly
attr_reader closing_loc: Location.
-
#locals ⇒ Object
readonly
attr_reader locals: Array.
-
#locals_body_index ⇒ Object
readonly
attr_reader locals_body_index: Integer.
-
#opening_loc ⇒ Object
readonly
attr_reader opening_loc: Location.
-
#operator_loc ⇒ Object
readonly
attr_reader operator_loc: Location.
-
#parameters ⇒ Object
readonly
attr_reader parameters: Node?.
Class Method Summary collapse
-
.type ⇒ Object
Similar to #type, this method returns a symbol that you can use for splitting on the type of the node without having to do a long === chain.
Instance Method Summary collapse
-
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void.
-
#child_nodes ⇒ Object
(also: #deconstruct)
def child_nodes: () -> Array[nil | Node].
-
#closing ⇒ Object
def closing: () -> String.
-
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location].
-
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array.
-
#copy(**params) ⇒ Object
def copy: (**params) -> LambdaNode.
- #deconstruct_keys(keys) ⇒ Object
-
#initialize(locals, locals_body_index, operator_loc, opening_loc, closing_loc, parameters, body, location) ⇒ LambdaNode
constructor
def initialize: (locals: Array, locals_body_index: Integer, operator_loc: Location, opening_loc: Location, closing_loc: Location, parameters: Node?, body: Node?, location: Location) -> void.
-
#inspect(inspector = NodeInspector.new) ⇒ Object
def inspect(inspector: NodeInspector) -> String.
-
#opening ⇒ Object
def opening: () -> String.
-
#operator ⇒ Object
def operator: () -> String.
-
#type ⇒ Object
Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform.
Constructor Details
#initialize(locals, locals_body_index, operator_loc, opening_loc, closing_loc, parameters, body, location) ⇒ LambdaNode
def initialize: (locals: Array, locals_body_index: Integer, operator_loc: Location, opening_loc: Location, closing_loc: Location, parameters: Node?, body: Node?, location: Location) -> void
10632 10633 10634 10635 10636 10637 10638 10639 10640 10641 |
# File 'lib/prism/node.rb', line 10632 def initialize(locals, locals_body_index, operator_loc, opening_loc, closing_loc, parameters, body, location) @locals = locals @locals_body_index = locals_body_index @operator_loc = operator_loc @opening_loc = opening_loc @closing_loc = closing_loc @parameters = parameters @body = body @location = location end |
Instance Attribute Details
#body ⇒ Object (readonly)
attr_reader body: Node?
10629 10630 10631 |
# File 'lib/prism/node.rb', line 10629 def body @body end |
#closing_loc ⇒ Object (readonly)
attr_reader closing_loc: Location
10623 10624 10625 |
# File 'lib/prism/node.rb', line 10623 def closing_loc @closing_loc end |
#locals ⇒ Object (readonly)
attr_reader locals: Array
10611 10612 10613 |
# File 'lib/prism/node.rb', line 10611 def locals @locals end |
#locals_body_index ⇒ Object (readonly)
attr_reader locals_body_index: Integer
10614 10615 10616 |
# File 'lib/prism/node.rb', line 10614 def locals_body_index @locals_body_index end |
#opening_loc ⇒ Object (readonly)
attr_reader opening_loc: Location
10620 10621 10622 |
# File 'lib/prism/node.rb', line 10620 def opening_loc @opening_loc end |
#operator_loc ⇒ Object (readonly)
attr_reader operator_loc: Location
10617 10618 10619 |
# File 'lib/prism/node.rb', line 10617 def operator_loc @operator_loc end |
#parameters ⇒ Object (readonly)
attr_reader parameters: Node?
10626 10627 10628 |
# File 'lib/prism/node.rb', line 10626 def parameters @parameters end |
Class Method Details
.type ⇒ Object
Similar to #type, this method returns a symbol that you can use for splitting on the type of the node without having to do a long === chain. Note that like #type, it will still be slower than using == for a single class, but should be faster in a case statement or an array comparison.
def self.type: () -> Symbol
10750 10751 10752 |
# File 'lib/prism/node.rb', line 10750 def self.type :lambda_node end |
Instance Method Details
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void
10644 10645 10646 |
# File 'lib/prism/node.rb', line 10644 def accept(visitor) visitor.visit_lambda_node(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
def child_nodes: () -> Array[nil | Node]
10649 10650 10651 |
# File 'lib/prism/node.rb', line 10649 def child_nodes [parameters, body] end |
#closing ⇒ Object
def closing: () -> String
10699 10700 10701 |
# File 'lib/prism/node.rb', line 10699 def closing closing_loc.slice end |
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location]
10662 10663 10664 |
# File 'lib/prism/node.rb', line 10662 def comment_targets [operator_loc, opening_loc, closing_loc, *parameters, *body] end |
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array
10654 10655 10656 10657 10658 10659 |
# File 'lib/prism/node.rb', line 10654 def compact_child_nodes compact = [] compact << parameters if parameters compact << body if body compact end |
#copy(**params) ⇒ Object
def copy: (**params) -> LambdaNode
10667 10668 10669 10670 10671 10672 10673 10674 10675 10676 10677 10678 |
# File 'lib/prism/node.rb', line 10667 def copy(**params) LambdaNode.new( params.fetch(:locals) { locals }, params.fetch(:locals_body_index) { locals_body_index }, params.fetch(:operator_loc) { operator_loc }, params.fetch(:opening_loc) { opening_loc }, params.fetch(:closing_loc) { closing_loc }, params.fetch(:parameters) { parameters }, params.fetch(:body) { body }, params.fetch(:location) { location }, ) end |
#deconstruct_keys(keys) ⇒ Object
10684 10685 10686 |
# File 'lib/prism/node.rb', line 10684 def deconstruct_keys(keys) { locals: locals, locals_body_index: locals_body_index, operator_loc: operator_loc, opening_loc: opening_loc, closing_loc: closing_loc, parameters: parameters, body: body, location: location } end |
#inspect(inspector = NodeInspector.new) ⇒ Object
def inspect(inspector: NodeInspector) -> String
10704 10705 10706 10707 10708 10709 10710 10711 10712 10713 10714 10715 10716 10717 10718 10719 10720 10721 10722 10723 10724 |
# File 'lib/prism/node.rb', line 10704 def inspect(inspector = NodeInspector.new) inspector << inspector.header(self) inspector << "├── locals: #{locals.inspect}\n" inspector << "├── locals_body_index: #{locals_body_index.inspect}\n" inspector << "├── operator_loc: #{inspector.location(operator_loc)}\n" inspector << "├── opening_loc: #{inspector.location(opening_loc)}\n" inspector << "├── closing_loc: #{inspector.location(closing_loc)}\n" if (parameters = self.parameters).nil? inspector << "├── parameters: ∅\n" else inspector << "├── parameters:\n" inspector << parameters.inspect(inspector.child_inspector("│ ")).delete_prefix(inspector.prefix) end if (body = self.body).nil? inspector << "└── body: ∅\n" else inspector << "└── body:\n" inspector << body.inspect(inspector.child_inspector(" ")).delete_prefix(inspector.prefix) end inspector.to_str end |
#opening ⇒ Object
def opening: () -> String
10694 10695 10696 |
# File 'lib/prism/node.rb', line 10694 def opening opening_loc.slice end |
#operator ⇒ Object
def operator: () -> String
10689 10690 10691 |
# File 'lib/prism/node.rb', line 10689 def operator operator_loc.slice end |
#type ⇒ Object
Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform. Usually this is done by calling ‘[cls1, cls2].include?(node.class)` or putting the node into a case statement and doing `case node; when cls1; when cls2; end`. Both of these approaches are relatively slow because of the constant lookups, method calls, and/or array allocations.
Instead, you can call #type, which will return to you a symbol that you can use for comparison. This is faster than the other approaches because it uses a single integer comparison, but also because if you’re on CRuby you can take advantage of the fact that case statements with all symbol keys will use a jump table.
def type: () -> Symbol
10740 10741 10742 |
# File 'lib/prism/node.rb', line 10740 def type :lambda_node end |