Class: Prism::InNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::InNode
- Defined in:
- lib/prism/node.rb,
ext/prism/api_node.c
Overview
Represents the use of the ‘in` keyword in a case statement.
case a; in b then c end
^^^^^^^^^^^
Instance Attribute Summary collapse
-
#in_loc ⇒ Object
readonly
attr_reader in_loc: Location.
-
#pattern ⇒ Object
readonly
attr_reader pattern: Node.
-
#statements ⇒ Object
readonly
attr_reader statements: StatementsNode?.
-
#then_loc ⇒ Object
readonly
attr_reader then_loc: Location?.
Instance Method Summary collapse
-
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void.
-
#child_nodes ⇒ Object
(also: #deconstruct)
def child_nodes: () -> Array[nil | Node].
-
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location].
-
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array.
-
#copy(**params) ⇒ Object
def copy: (**params) -> InNode.
- #deconstruct_keys(keys) ⇒ Object
-
#in ⇒ Object
def in: () -> String.
-
#initialize(pattern, statements, in_loc, then_loc, location) ⇒ InNode
constructor
def initialize: (pattern: Node, statements: StatementsNode?, in_loc: Location, then_loc: Location?, location: Location) -> void.
- #inspect(inspector = NodeInspector.new) ⇒ Object
-
#then ⇒ Object
def then: () -> 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(pattern, statements, in_loc, then_loc, location) ⇒ InNode
def initialize: (pattern: Node, statements: StatementsNode?, in_loc: Location, then_loc: Location?, location: Location) -> void
7055 7056 7057 7058 7059 7060 7061 |
# File 'lib/prism/node.rb', line 7055 def initialize(pattern, statements, in_loc, then_loc, location) @pattern = pattern @statements = statements @in_loc = in_loc @then_loc = then_loc @location = location end |
Instance Attribute Details
#in_loc ⇒ Object (readonly)
attr_reader in_loc: Location
7049 7050 7051 |
# File 'lib/prism/node.rb', line 7049 def in_loc @in_loc end |
#pattern ⇒ Object (readonly)
attr_reader pattern: Node
7043 7044 7045 |
# File 'lib/prism/node.rb', line 7043 def pattern @pattern end |
#statements ⇒ Object (readonly)
attr_reader statements: StatementsNode?
7046 7047 7048 |
# File 'lib/prism/node.rb', line 7046 def statements @statements end |
#then_loc ⇒ Object (readonly)
attr_reader then_loc: Location?
7052 7053 7054 |
# File 'lib/prism/node.rb', line 7052 def then_loc @then_loc end |
Instance Method Details
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void
7064 7065 7066 |
# File 'lib/prism/node.rb', line 7064 def accept(visitor) visitor.visit_in_node(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
def child_nodes: () -> Array[nil | Node]
7069 7070 7071 |
# File 'lib/prism/node.rb', line 7069 def child_nodes [pattern, statements] end |
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location]
7082 7083 7084 |
# File 'lib/prism/node.rb', line 7082 def comment_targets [pattern, *statements, in_loc, *then_loc] end |
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array
7074 7075 7076 7077 7078 7079 |
# File 'lib/prism/node.rb', line 7074 def compact_child_nodes compact = [] compact << pattern compact << statements if statements compact end |
#copy(**params) ⇒ Object
def copy: (**params) -> InNode
7087 7088 7089 7090 7091 7092 7093 7094 7095 |
# File 'lib/prism/node.rb', line 7087 def copy(**params) InNode.new( params.fetch(:pattern) { pattern }, params.fetch(:statements) { statements }, params.fetch(:in_loc) { in_loc }, params.fetch(:then_loc) { then_loc }, params.fetch(:location) { location }, ) end |
#deconstruct_keys(keys) ⇒ Object
7101 7102 7103 |
# File 'lib/prism/node.rb', line 7101 def deconstruct_keys(keys) { pattern: pattern, statements: statements, in_loc: in_loc, then_loc: then_loc, location: location } end |
#in ⇒ Object
def in: () -> String
7106 7107 7108 |
# File 'lib/prism/node.rb', line 7106 def in in_loc.slice end |
#inspect(inspector = NodeInspector.new) ⇒ Object
7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 |
# File 'lib/prism/node.rb', line 7115 def inspect(inspector = NodeInspector.new) inspector << inspector.header(self) inspector << "├── pattern:\n" inspector << inspector.child_node(pattern, "│ ") if (statements = self.statements).nil? inspector << "├── statements: ∅\n" else inspector << "├── statements:\n" inspector << statements.inspect(inspector.child_inspector("│ ")).delete_prefix(inspector.prefix) end inspector << "├── in_loc: #{inspector.location(in_loc)}\n" inspector << "└── then_loc: #{inspector.location(then_loc)}\n" inspector.to_str end |
#then ⇒ Object
def then: () -> String?
7111 7112 7113 |
# File 'lib/prism/node.rb', line 7111 def then then_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
7144 7145 7146 |
# File 'lib/prism/node.rb', line 7144 def type :in_node end |