Class: Prism::BeginNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::BeginNode
- Defined in:
- lib/prism/node.rb,
ext/prism/api_node.c
Overview
Represents a begin statement.
begin
foo
end
^^^^^
Instance Attribute Summary collapse
-
#begin_keyword_loc ⇒ Object
readonly
attr_reader begin_keyword_loc: Location?.
-
#else_clause ⇒ Object
readonly
attr_reader else_clause: ElseNode?.
-
#end_keyword_loc ⇒ Object
readonly
attr_reader end_keyword_loc: Location?.
-
#ensure_clause ⇒ Object
readonly
attr_reader ensure_clause: EnsureNode?.
-
#rescue_clause ⇒ Object
readonly
attr_reader rescue_clause: RescueNode?.
-
#statements ⇒ Object
readonly
attr_reader statements: StatementsNode?.
Instance Method Summary collapse
-
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void.
-
#begin_keyword ⇒ Object
def begin_keyword: () -> String?.
-
#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) -> BeginNode.
- #deconstruct_keys(keys) ⇒ Object
-
#end_keyword ⇒ Object
def end_keyword: () -> String?.
-
#initialize(begin_keyword_loc, statements, rescue_clause, else_clause, ensure_clause, end_keyword_loc, location) ⇒ BeginNode
constructor
def initialize: (begin_keyword_loc: Location?, statements: StatementsNode?, rescue_clause: RescueNode?, else_clause: ElseNode?, ensure_clause: EnsureNode?, end_keyword_loc: Location?, location: Location) -> void.
- #inspect(inspector = NodeInspector.new) ⇒ Object
- #set_newline_flag(newline_marked) ⇒ Object
-
#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(begin_keyword_loc, statements, rescue_clause, else_clause, ensure_clause, end_keyword_loc, location) ⇒ BeginNode
def initialize: (begin_keyword_loc: Location?, statements: StatementsNode?, rescue_clause: RescueNode?, else_clause: ElseNode?, ensure_clause: EnsureNode?, end_keyword_loc: Location?, location: Location) -> void
1021 1022 1023 1024 1025 1026 1027 1028 1029 |
# File 'lib/prism/node.rb', line 1021 def initialize(begin_keyword_loc, statements, rescue_clause, else_clause, ensure_clause, end_keyword_loc, location) @begin_keyword_loc = begin_keyword_loc @statements = statements @rescue_clause = rescue_clause @else_clause = else_clause @ensure_clause = ensure_clause @end_keyword_loc = end_keyword_loc @location = location end |
Instance Attribute Details
#begin_keyword_loc ⇒ Object (readonly)
attr_reader begin_keyword_loc: Location?
1003 1004 1005 |
# File 'lib/prism/node.rb', line 1003 def begin_keyword_loc @begin_keyword_loc end |
#else_clause ⇒ Object (readonly)
attr_reader else_clause: ElseNode?
1012 1013 1014 |
# File 'lib/prism/node.rb', line 1012 def else_clause @else_clause end |
#end_keyword_loc ⇒ Object (readonly)
attr_reader end_keyword_loc: Location?
1018 1019 1020 |
# File 'lib/prism/node.rb', line 1018 def end_keyword_loc @end_keyword_loc end |
#ensure_clause ⇒ Object (readonly)
attr_reader ensure_clause: EnsureNode?
1015 1016 1017 |
# File 'lib/prism/node.rb', line 1015 def ensure_clause @ensure_clause end |
#rescue_clause ⇒ Object (readonly)
attr_reader rescue_clause: RescueNode?
1009 1010 1011 |
# File 'lib/prism/node.rb', line 1009 def rescue_clause @rescue_clause end |
#statements ⇒ Object (readonly)
attr_reader statements: StatementsNode?
1006 1007 1008 |
# File 'lib/prism/node.rb', line 1006 def statements @statements end |
Instance Method Details
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void
1032 1033 1034 |
# File 'lib/prism/node.rb', line 1032 def accept(visitor) visitor.visit_begin_node(self) end |
#begin_keyword ⇒ Object
def begin_keyword: () -> String?
1082 1083 1084 |
# File 'lib/prism/node.rb', line 1082 def begin_keyword begin_keyword_loc&.slice end |
#child_nodes ⇒ Object Also known as: deconstruct
def child_nodes: () -> Array[nil | Node]
1041 1042 1043 |
# File 'lib/prism/node.rb', line 1041 def child_nodes [statements, rescue_clause, else_clause, ensure_clause] end |
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location]
1056 1057 1058 |
# File 'lib/prism/node.rb', line 1056 def comment_targets [*begin_keyword_loc, *statements, *rescue_clause, *else_clause, *ensure_clause, *end_keyword_loc] end |
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array
1046 1047 1048 1049 1050 1051 1052 1053 |
# File 'lib/prism/node.rb', line 1046 def compact_child_nodes compact = [] compact << statements if statements compact << rescue_clause if rescue_clause compact << else_clause if else_clause compact << ensure_clause if ensure_clause compact end |
#copy(**params) ⇒ Object
def copy: (**params) -> BeginNode
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 |
# File 'lib/prism/node.rb', line 1061 def copy(**params) BeginNode.new( params.fetch(:begin_keyword_loc) { begin_keyword_loc }, params.fetch(:statements) { statements }, params.fetch(:rescue_clause) { rescue_clause }, params.fetch(:else_clause) { else_clause }, params.fetch(:ensure_clause) { ensure_clause }, params.fetch(:end_keyword_loc) { end_keyword_loc }, params.fetch(:location) { location }, ) end |
#deconstruct_keys(keys) ⇒ Object
1077 1078 1079 |
# File 'lib/prism/node.rb', line 1077 def deconstruct_keys(keys) { begin_keyword_loc: begin_keyword_loc, statements: statements, rescue_clause: rescue_clause, else_clause: else_clause, ensure_clause: ensure_clause, end_keyword_loc: end_keyword_loc, location: location } end |
#end_keyword ⇒ Object
def end_keyword: () -> String?
1087 1088 1089 |
# File 'lib/prism/node.rb', line 1087 def end_keyword end_keyword_loc&.slice end |
#inspect(inspector = NodeInspector.new) ⇒ Object
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 |
# File 'lib/prism/node.rb', line 1091 def inspect(inspector = NodeInspector.new) inspector << inspector.header(self) inspector << "├── begin_keyword_loc: #{inspector.location(begin_keyword_loc)}\n" if (statements = self.statements).nil? inspector << "├── statements: ∅\n" else inspector << "├── statements:\n" inspector << statements.inspect(inspector.child_inspector("│ ")).delete_prefix(inspector.prefix) end if (rescue_clause = self.rescue_clause).nil? inspector << "├── rescue_clause: ∅\n" else inspector << "├── rescue_clause:\n" inspector << rescue_clause.inspect(inspector.child_inspector("│ ")).delete_prefix(inspector.prefix) end if (else_clause = self.else_clause).nil? inspector << "├── else_clause: ∅\n" else inspector << "├── else_clause:\n" inspector << else_clause.inspect(inspector.child_inspector("│ ")).delete_prefix(inspector.prefix) end if (ensure_clause = self.ensure_clause).nil? inspector << "├── ensure_clause: ∅\n" else inspector << "├── ensure_clause:\n" inspector << ensure_clause.inspect(inspector.child_inspector("│ ")).delete_prefix(inspector.prefix) end inspector << "└── end_keyword_loc: #{inspector.location(end_keyword_loc)}\n" inspector.to_str end |
#set_newline_flag(newline_marked) ⇒ Object
1036 1037 1038 |
# File 'lib/prism/node.rb', line 1036 def set_newline_flag(newline_marked) # Never mark BeginNode with a newline flag, mark children instead 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
1136 1137 1138 |
# File 'lib/prism/node.rb', line 1136 def type :begin_node end |