Class: Prism::RangeNode

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

Overview

Represents the use of the .. or ... operators.

1..2
^^^^

c if a =~ /left/ ... b =~ /right/
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(left, right, operator_loc, flags, location) ⇒ RangeNode

def initialize: (left: Node?, right: Node?, operator_loc: Location, flags: Integer, location: Location) -> void



11508
11509
11510
11511
11512
11513
11514
# File 'lib/prism/node.rb', line 11508

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

Instance Attribute Details

#leftObject (readonly)

attr_reader left: Node?



11496
11497
11498
# File 'lib/prism/node.rb', line 11496

def left
  @left
end

#operator_locObject (readonly)

attr_reader operator_loc: Location



11502
11503
11504
# File 'lib/prism/node.rb', line 11502

def operator_loc
  @operator_loc
end

#rightObject (readonly)

attr_reader right: Node?



11499
11500
11501
# File 'lib/prism/node.rb', line 11499

def right
  @right
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



11517
11518
11519
# File 'lib/prism/node.rb', line 11517

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

#child_nodesObject Also known as: deconstruct

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



11522
11523
11524
# File 'lib/prism/node.rb', line 11522

def child_nodes
  [left, right]
end

#comment_targetsObject

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



11535
11536
11537
# File 'lib/prism/node.rb', line 11535

def comment_targets
  [*left, *right, operator_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



11527
11528
11529
11530
11531
11532
# File 'lib/prism/node.rb', line 11527

def compact_child_nodes
  compact = []
  compact << left if left
  compact << right if right
  compact
end

#copy(**params) ⇒ Object

def copy: (**params) -> RangeNode



11540
11541
11542
11543
11544
11545
11546
11547
11548
# File 'lib/prism/node.rb', line 11540

def copy(**params)
  RangeNode.new(
    params.fetch(:left) { left },
    params.fetch(:right) { right },
    params.fetch(:operator_loc) { operator_loc },
    params.fetch(:flags) { flags },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

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



11554
11555
11556
# File 'lib/prism/node.rb', line 11554

def deconstruct_keys(keys)
  { left: left, right: right, operator_loc: operator_loc, flags: flags, location: location }
end

#exclude_end?Boolean

def exclude_end?: () -> bool

Returns:

  • (Boolean)


11564
11565
11566
# File 'lib/prism/node.rb', line 11564

def exclude_end?
  flags.anybits?(RangeFlags::EXCLUDE_END)
end

#inspect(inspector = NodeInspector.new) ⇒ Object



11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
# File 'lib/prism/node.rb', line 11568

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  if (left = self.left).nil?
    inspector << "├── left: ∅\n"
  else
    inspector << "├── left:\n"
    inspector << left.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  if (right = self.right).nil?
    inspector << "├── right: ∅\n"
  else
    inspector << "├── right:\n"
    inspector << right.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  inspector << "├── operator_loc: #{inspector.location(operator_loc)}\n"
  flags = [("exclude_end" if exclude_end?)].compact
  inspector << "└── flags: #{flags.empty? ? "" : flags.join(", ")}\n"
  inspector.to_str
end

#operatorObject

def operator: () -> String



11559
11560
11561
# File 'lib/prism/node.rb', line 11559

def operator
  operator_loc.slice
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



11602
11603
11604
# File 'lib/prism/node.rb', line 11602

def type
  :range_node
end