Class: Prism::AndNode

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

Overview

Represents the use of the && operator or the and keyword.

left and right
^^^^^^^^^^^^^^

Explore code sample

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, left, right, operator_loc) ⇒ AndNode

Initialize a new AndNode node.

: (Source source, Integer node_id, Location location, Integer flags, Prism::node left, Prism::node right, Location operator_loc) -> void

Parameters:

  • source (Source)
  • node_id (Integer)
  • location (Location)
  • flags (Integer)
  • left (Prism::node)
  • right (Prism::node)
  • operator_loc (Location)


1057
1058
1059
1060
1061
1062
1063
1064
1065
# File 'lib/prism/node.rb', line 1057

def initialize(source, node_id, location, flags, left, right, operator_loc)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @left = left
  @right = right
  @operator_loc = operator_loc
end

Class Method Details

.type ⇒ :and_node

See Node.type.

: () -> :and_node

Returns:

  • (:and_node)


1148
1149
1150
# File 'lib/prism/node.rb', line 1148

def self.type
  :and_node
end

Instance Method Details

#===(other) ⇒ boolish

: (untyped other) -> boolish

Parameters:

  • other (Object)

Returns:

  • (boolish)


1243
1244
1245
1246
1247
1248
# File 'lib/prism/node.rb', line 1243

def ===(other) # :nodoc:
  other.is_a?(AndNode) &&
    (left === other.left) &&
    (right === other.right) &&
    (operator_loc.nil? == other.operator_loc.nil?)
end

#accept(visitor) ⇒ Object

See Node.accept.

: (_Visitor visitor) -> untyped

Parameters:

Returns:

  • (Object)


1081
1082
1083
# File 'lib/prism/node.rb', line 1081

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

#child_nodes ⇒ Array[node?] Also known as: deconstruct

See Node.child_nodes.

: () -> Array

Returns:

  • (Array[node?])


1088
1089
1090
# File 'lib/prism/node.rb', line 1088

def child_nodes
  [left, right]
end

#compact_child_nodes ⇒ Array[node]

See Node.compact_child_nodes.

: () -> Array

Returns:

  • (Array[node])


1106
1107
1108
# File 'lib/prism/node.rb', line 1106

def compact_child_nodes
  [left, right]
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, left: self.left, right: self.right, operator_loc: self.operator_loc) ⇒ AndNode

:call-seq:

copy(**fields) -> AndNode

Creates a copy of self with the given fields, using self as the template.

: (?node_id: Integer, ?location: Location, ?flags: Integer, ?left: Prism::node, ?right: Prism::node, ?operator_loc: Location) -> AndNode

Parameters:

  • node_id: (Integer) (defaults to: self.node_id)
  • location: (Location) (defaults to: self.location)
  • flags: (Integer) (defaults to: self.flags)
  • left: (Prism::node) (defaults to: self.left)
  • right: (Prism::node) (defaults to: self.right)
  • operator_loc: (Location) (defaults to: self.operator_loc)

Returns:



1116
1117
1118
# File 'lib/prism/node.rb', line 1116

def copy(node_id: self.node_id, location: self.location, flags: self.flags, left: self.left, right: self.right, operator_loc: self.operator_loc)
  AndNode.new(source, node_id, location, flags, left, right, operator_loc)
end

#deconstruct_keys(keys) ⇒ Hash[Symbol, untyped]

: (Array? keys) -> Hash[Symbol, untyped]

Parameters:

  • keys (Array[Symbol], nil)

Returns:

  • (Hash[Symbol, untyped])


1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
# File 'lib/prism/node.rb', line 1123

def deconstruct_keys(keys) # :nodoc:
  (keys || %i[node_id location left right operator_loc operator]).each_with_object(
    {} #: Hash[Symbol, untyped]
  ) do |key, deconstructed|
    case key
    when :node_id                 then deconstructed[:node_id] = self.node_id
    when :location                then deconstructed[:location] = self.location
    when :left                    then deconstructed[:left] = self.left
    when :right                   then deconstructed[:right] = self.right
    when :operator_loc            then deconstructed[:operator_loc] = self.operator_loc
    when :operator                then deconstructed[:operator] = self.operator
    end
  end
end

#each_child_node ⇒ Enumerator[node, void] #each_child_node ⇒ void

See Node.each_child_node.

: () -> Enumerator[node, void] : () { (node) -> void } -> void

Overloads:

  • #each_child_node ⇒ Enumerator[node, void]

    Returns:

    • (Enumerator[node, void])
  • #each_child_node ⇒ void

    This method returns an undefined value.

Yields:

Yield Parameters:

  • arg0 (node)

Yield Returns:

  • (void)


1096
1097
1098
1099
1100
1101
# File 'lib/prism/node.rb', line 1096

def each_child_node(&blk)
  return to_enum(:each_child_node) unless block_given?

  yield left
  yield right
end

#inspect ⇒ String

: () -> String

Returns:

  • (String)


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

def inspect # :nodoc:
  InspectVisitor.compose(self)
end

#left ⇒ Prism::node

:call-seq:

left -> Node

Represents the left side of the expression. It can be any non-void expression.

left and right
^^^^

1 && 2
^

Explore code sample

: () -> Prism::node

Returns:

  • (Prism::node)


1175
1176
1177
# File 'lib/prism/node.rb', line 1175

def left
  @left
end

#operator ⇒ String

:call-seq:

operator -> String

Slice the location of operator_loc from the source.

: () -> String

Returns:

  • (String)


1236
1237
1238
# File 'lib/prism/node.rb', line 1236

def operator
  operator_loc.slice
end

#operator_loc ⇒ Location

:category: Locations :call-seq:

operator_loc -> Location

The Location of the and keyword or the && operator.

left and right
     ^^^

Explore code sample

: () -> Location

Returns:



1213
1214
1215
1216
1217
# File 'lib/prism/node.rb', line 1213

def operator_loc
  location = @operator_loc
  return location if location.is_a?(Location)
  @operator_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

#right ⇒ Prism::node

:call-seq:

right -> Node

Represents the right side of the expression.

left && right
        ^^^^^

1 and 2
      ^

Explore code sample

: () -> Prism::node

Returns:

  • (Prism::node)


1195
1196
1197
# File 'lib/prism/node.rb', line 1195

def right
  @right
end

#save_operator_loc(repository) ⇒ Relocation::Entry

:category: Repository Save the operator_loc location using the given saved source so that it can be retrieved later.

: (_Repository repository) -> Relocation::Entry

Parameters:

Returns:



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

def save_operator_loc(repository)
  repository.enter(node_id, :operator_loc)
end

#type ⇒ :and_node

See Node#type.

: () -> :and_node

Returns:

  • (:and_node)


1141
1142
1143
# File 'lib/prism/node.rb', line 1141

def type
  :and_node
end