Class: Prism::BeginNode

Inherits:
PrismNode
  • Object
show all
Defined in:
lib/prism/node.rb,
ext/prism/api_node.c

Overview

Represents a begin statement.

begin
  foo
end
^^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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



1168
1169
1170
1171
1172
1173
1174
1175
1176
# File 'lib/prism/node.rb', line 1168

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_locObject (readonly)

attr_reader begin_keyword_loc: Location?



1150
1151
1152
# File 'lib/prism/node.rb', line 1150

def begin_keyword_loc
  @begin_keyword_loc
end

#else_clauseObject (readonly)

attr_reader else_clause: ElseNode?



1159
1160
1161
# File 'lib/prism/node.rb', line 1159

def else_clause
  @else_clause
end

#end_keyword_locObject (readonly)

attr_reader end_keyword_loc: Location?



1165
1166
1167
# File 'lib/prism/node.rb', line 1165

def end_keyword_loc
  @end_keyword_loc
end

#ensure_clauseObject (readonly)

attr_reader ensure_clause: EnsureNode?



1162
1163
1164
# File 'lib/prism/node.rb', line 1162

def ensure_clause
  @ensure_clause
end

#rescue_clauseObject (readonly)

attr_reader rescue_clause: RescueNode?



1156
1157
1158
# File 'lib/prism/node.rb', line 1156

def rescue_clause
  @rescue_clause
end

#statementsObject (readonly)

attr_reader statements: StatementsNode?



1153
1154
1155
# File 'lib/prism/node.rb', line 1153

def statements
  @statements
end

Class Method Details

.typeObject

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



1294
1295
1296
# File 'lib/prism/node.rb', line 1294

def self.type
  :begin_node
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



1179
1180
1181
# File 'lib/prism/node.rb', line 1179

def accept(visitor)
  visitor.visit_begin_node(self)
end

#begin_keywordObject

def begin_keyword: () -> String?



1229
1230
1231
# File 'lib/prism/node.rb', line 1229

def begin_keyword
  begin_keyword_loc&.slice
end

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array[nil | Node]



1188
1189
1190
# File 'lib/prism/node.rb', line 1188

def child_nodes
  [statements, rescue_clause, else_clause, ensure_clause]
end

#comment_targetsObject

def comment_targets: () -> Array[Node | Location]



1203
1204
1205
# File 'lib/prism/node.rb', line 1203

def comment_targets
  [*begin_keyword_loc, *statements, *rescue_clause, *else_clause, *ensure_clause, *end_keyword_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



1193
1194
1195
1196
1197
1198
1199
1200
# File 'lib/prism/node.rb', line 1193

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



1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
# File 'lib/prism/node.rb', line 1208

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

def deconstruct_keys: (keys: Array) -> Hash[Symbol, nil | Node | Array | String | Token | Array | Location]



1224
1225
1226
# File 'lib/prism/node.rb', line 1224

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_keywordObject

def end_keyword: () -> String?



1234
1235
1236
# File 'lib/prism/node.rb', line 1234

def end_keyword
  end_keyword_loc&.slice
end

#inspect(inspector = NodeInspector.new) ⇒ Object

def inspect(inspector: NodeInspector) -> String



1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
# File 'lib/prism/node.rb', line 1239

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

:nodoc:



1183
1184
1185
# File 'lib/prism/node.rb', line 1183

def set_newline_flag(newline_marked) # :nodoc:
  # Never mark BeginNode with a newline flag, mark children instead
end

#typeObject

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



1284
1285
1286
# File 'lib/prism/node.rb', line 1284

def type
  :begin_node
end