Class: Prism::WhileNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::WhileNode
- Defined in:
- lib/prism/node.rb,
ext/prism/api_node.c
Overview
Represents the use of the ‘while` keyword, either in the block form or the modifier form.
bar while foo
^^^^^^^^^^^^^
while foo do bar end
^^^^^^^^^^^^^^^^^^^^
Instance Attribute Summary collapse
-
#closing_loc ⇒ Object
readonly
attr_reader closing_loc: Location?.
-
#flags ⇒ Object
readonly
Returns the value of attribute flags.
-
#keyword_loc ⇒ Object
readonly
attr_reader keyword_loc: Location.
-
#predicate ⇒ Object
readonly
attr_reader predicate: Node.
-
#statements ⇒ Object
readonly
attr_reader statements: StatementsNode?.
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.
-
#begin_modifier? ⇒ Boolean
def begin_modifier?: () -> bool.
-
#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) -> WhileNode.
- #deconstruct_keys(keys) ⇒ Object
-
#initialize(flags, keyword_loc, closing_loc, predicate, statements, location) ⇒ WhileNode
constructor
def initialize: (flags: Integer, keyword_loc: Location, closing_loc: Location?, predicate: Node, statements: StatementsNode?, location: Location) -> void.
-
#inspect(inspector = NodeInspector.new) ⇒ Object
def inspect(inspector: NodeInspector) -> String.
-
#keyword ⇒ Object
def keyword: () -> String.
-
#set_newline_flag(newline_marked) ⇒ Object
:nodoc:.
-
#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(flags, keyword_loc, closing_loc, predicate, statements, location) ⇒ WhileNode
def initialize: (flags: Integer, keyword_loc: Location, closing_loc: Location?, predicate: Node, statements: StatementsNode?, location: Location) -> void
16896 16897 16898 16899 16900 16901 16902 16903 |
# File 'lib/prism/node.rb', line 16896 def initialize(flags, keyword_loc, closing_loc, predicate, statements, location) @flags = flags @keyword_loc = keyword_loc @closing_loc = closing_loc @predicate = predicate @statements = statements @location = location end |
Instance Attribute Details
#closing_loc ⇒ Object (readonly)
attr_reader closing_loc: Location?
16887 16888 16889 |
# File 'lib/prism/node.rb', line 16887 def closing_loc @closing_loc end |
#flags ⇒ Object (readonly)
Returns the value of attribute flags.
16881 16882 16883 |
# File 'lib/prism/node.rb', line 16881 def flags @flags end |
#keyword_loc ⇒ Object (readonly)
attr_reader keyword_loc: Location
16884 16885 16886 |
# File 'lib/prism/node.rb', line 16884 def keyword_loc @keyword_loc end |
#predicate ⇒ Object (readonly)
attr_reader predicate: Node
16890 16891 16892 |
# File 'lib/prism/node.rb', line 16890 def predicate @predicate end |
#statements ⇒ Object (readonly)
attr_reader statements: StatementsNode?
16893 16894 16895 |
# File 'lib/prism/node.rb', line 16893 def statements @statements 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
17009 17010 17011 |
# File 'lib/prism/node.rb', line 17009 def self.type :while_node end |
Instance Method Details
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void
16906 16907 16908 |
# File 'lib/prism/node.rb', line 16906 def accept(visitor) visitor.visit_while_node(self) end |
#begin_modifier? ⇒ Boolean
def begin_modifier?: () -> bool
16953 16954 16955 |
# File 'lib/prism/node.rb', line 16953 def begin_modifier? flags.anybits?(LoopFlags::BEGIN_MODIFIER) end |
#child_nodes ⇒ Object Also known as: deconstruct
def child_nodes: () -> Array[nil | Node]
16915 16916 16917 |
# File 'lib/prism/node.rb', line 16915 def child_nodes [predicate, statements] end |
#closing ⇒ Object
def closing: () -> String?
16963 16964 16965 |
# File 'lib/prism/node.rb', line 16963 def closing closing_loc&.slice end |
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location]
16928 16929 16930 |
# File 'lib/prism/node.rb', line 16928 def comment_targets [keyword_loc, *closing_loc, predicate, *statements] end |
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array
16920 16921 16922 16923 16924 16925 |
# File 'lib/prism/node.rb', line 16920 def compact_child_nodes compact = [] compact << predicate compact << statements if statements compact end |
#copy(**params) ⇒ Object
def copy: (**params) -> WhileNode
16933 16934 16935 16936 16937 16938 16939 16940 16941 16942 |
# File 'lib/prism/node.rb', line 16933 def copy(**params) WhileNode.new( params.fetch(:flags) { flags }, params.fetch(:keyword_loc) { keyword_loc }, params.fetch(:closing_loc) { closing_loc }, params.fetch(:predicate) { predicate }, params.fetch(:statements) { statements }, params.fetch(:location) { location }, ) end |
#deconstruct_keys(keys) ⇒ Object
16948 16949 16950 |
# File 'lib/prism/node.rb', line 16948 def deconstruct_keys(keys) { flags: flags, keyword_loc: keyword_loc, closing_loc: closing_loc, predicate: predicate, statements: statements, location: location } end |
#inspect(inspector = NodeInspector.new) ⇒ Object
def inspect(inspector: NodeInspector) -> String
16968 16969 16970 16971 16972 16973 16974 16975 16976 16977 16978 16979 16980 16981 16982 16983 |
# File 'lib/prism/node.rb', line 16968 def inspect(inspector = NodeInspector.new) inspector << inspector.header(self) flags = [("begin_modifier" if begin_modifier?)].compact inspector << "├── flags: #{flags.empty? ? "∅" : flags.join(", ")}\n" inspector << "├── keyword_loc: #{inspector.location(keyword_loc)}\n" inspector << "├── closing_loc: #{inspector.location(closing_loc)}\n" inspector << "├── predicate:\n" inspector << inspector.child_node(predicate, "│ ") if (statements = self.statements).nil? inspector << "└── statements: ∅\n" else inspector << "└── statements:\n" inspector << statements.inspect(inspector.child_inspector(" ")).delete_prefix(inspector.prefix) end inspector.to_str end |
#keyword ⇒ Object
def keyword: () -> String
16958 16959 16960 |
# File 'lib/prism/node.rb', line 16958 def keyword keyword_loc.slice end |
#set_newline_flag(newline_marked) ⇒ Object
:nodoc:
16910 16911 16912 |
# File 'lib/prism/node.rb', line 16910 def set_newline_flag(newline_marked) # :nodoc: predicate.set_newline_flag(newline_marked) 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
16999 17000 17001 |
# File 'lib/prism/node.rb', line 16999 def type :while_node end |