Class: Prism::BlockNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::BlockNode
- Defined in:
- lib/prism/node.rb,
ext/prism/api_node.c
Overview
Represents a block of ruby code.
[1, 2, 3].each { |i| puts x }
^^^^^^^^^^^^^^
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.
-
#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) -> BlockNode.
- #deconstruct_keys(keys) ⇒ Object
-
#initialize(locals, locals_body_index, parameters, body, opening_loc, closing_loc, location) ⇒ BlockNode
constructor
def initialize: (locals: Array, locals_body_index: Integer, parameters: Node?, body: Node?, opening_loc: Location, closing_loc: Location, location: Location) -> void.
-
#inspect(inspector = NodeInspector.new) ⇒ Object
def inspect(inspector: NodeInspector) -> String.
-
#opening ⇒ Object
def opening: () -> 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, parameters, body, opening_loc, closing_loc, location) ⇒ BlockNode
def initialize: (locals: Array, locals_body_index: Integer, parameters: Node?, body: Node?, opening_loc: Location, closing_loc: Location, location: Location) -> void
1513 1514 1515 1516 1517 1518 1519 1520 1521 |
# File 'lib/prism/node.rb', line 1513 def initialize(locals, locals_body_index, parameters, body, opening_loc, closing_loc, location) @locals = locals @locals_body_index = locals_body_index @parameters = parameters @body = body @opening_loc = opening_loc @closing_loc = closing_loc @location = location end |
Instance Attribute Details
#body ⇒ Object (readonly)
attr_reader body: Node?
1504 1505 1506 |
# File 'lib/prism/node.rb', line 1504 def body @body end |
#closing_loc ⇒ Object (readonly)
attr_reader closing_loc: Location
1510 1511 1512 |
# File 'lib/prism/node.rb', line 1510 def closing_loc @closing_loc end |
#locals ⇒ Object (readonly)
attr_reader locals: Array
1495 1496 1497 |
# File 'lib/prism/node.rb', line 1495 def locals @locals end |
#locals_body_index ⇒ Object (readonly)
attr_reader locals_body_index: Integer
1498 1499 1500 |
# File 'lib/prism/node.rb', line 1498 def locals_body_index @locals_body_index end |
#opening_loc ⇒ Object (readonly)
attr_reader opening_loc: Location
1507 1508 1509 |
# File 'lib/prism/node.rb', line 1507 def opening_loc @opening_loc end |
#parameters ⇒ Object (readonly)
attr_reader parameters: Node?
1501 1502 1503 |
# File 'lib/prism/node.rb', line 1501 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
1623 1624 1625 |
# File 'lib/prism/node.rb', line 1623 def self.type :block_node end |
Instance Method Details
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void
1524 1525 1526 |
# File 'lib/prism/node.rb', line 1524 def accept(visitor) visitor.visit_block_node(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
def child_nodes: () -> Array[nil | Node]
1529 1530 1531 |
# File 'lib/prism/node.rb', line 1529 def child_nodes [parameters, body] end |
#closing ⇒ Object
def closing: () -> String
1573 1574 1575 |
# File 'lib/prism/node.rb', line 1573 def closing closing_loc.slice end |
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location]
1542 1543 1544 |
# File 'lib/prism/node.rb', line 1542 def comment_targets [*parameters, *body, opening_loc, closing_loc] end |
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array
1534 1535 1536 1537 1538 1539 |
# File 'lib/prism/node.rb', line 1534 def compact_child_nodes compact = [] compact << parameters if parameters compact << body if body compact end |
#copy(**params) ⇒ Object
def copy: (**params) -> BlockNode
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 |
# File 'lib/prism/node.rb', line 1547 def copy(**params) BlockNode.new( params.fetch(:locals) { locals }, params.fetch(:locals_body_index) { locals_body_index }, params.fetch(:parameters) { parameters }, params.fetch(:body) { body }, params.fetch(:opening_loc) { opening_loc }, params.fetch(:closing_loc) { closing_loc }, params.fetch(:location) { location }, ) end |
#deconstruct_keys(keys) ⇒ Object
1563 1564 1565 |
# File 'lib/prism/node.rb', line 1563 def deconstruct_keys(keys) { locals: locals, locals_body_index: locals_body_index, parameters: parameters, body: body, opening_loc: opening_loc, closing_loc: closing_loc, location: location } end |
#inspect(inspector = NodeInspector.new) ⇒ Object
def inspect(inspector: NodeInspector) -> String
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 |
# File 'lib/prism/node.rb', line 1578 def inspect(inspector = NodeInspector.new) inspector << inspector.header(self) inspector << "├── locals: #{locals.inspect}\n" inspector << "├── locals_body_index: #{locals_body_index.inspect}\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 << "├── opening_loc: #{inspector.location(opening_loc)}\n" inspector << "└── closing_loc: #{inspector.location(closing_loc)}\n" inspector.to_str end |
#opening ⇒ Object
def opening: () -> String
1568 1569 1570 |
# File 'lib/prism/node.rb', line 1568 def opening opening_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
1613 1614 1615 |
# File 'lib/prism/node.rb', line 1613 def type :block_node end |