Class: Prism::SingletonClassNode

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

Overview

Represents a singleton class declaration involving the class keyword.

class << self end
^^^^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locals, class_keyword_loc, operator_loc, expression, body, end_keyword_loc, location) ⇒ SingletonClassNode

def initialize: (locals: Array, class_keyword_loc: Location, operator_loc: Location, expression: Node, body: Node?, end_keyword_loc: Location, location: Location) -> void



12668
12669
12670
12671
12672
12673
12674
12675
12676
# File 'lib/prism/node.rb', line 12668

def initialize(locals, class_keyword_loc, operator_loc, expression, body, end_keyword_loc, location)
  @locals = locals
  @class_keyword_loc = class_keyword_loc
  @operator_loc = operator_loc
  @expression = expression
  @body = body
  @end_keyword_loc = end_keyword_loc
  @location = location
end

Instance Attribute Details

#bodyObject (readonly)

attr_reader body: Node?



12662
12663
12664
# File 'lib/prism/node.rb', line 12662

def body
  @body
end

#class_keyword_locObject (readonly)

attr_reader class_keyword_loc: Location



12653
12654
12655
# File 'lib/prism/node.rb', line 12653

def class_keyword_loc
  @class_keyword_loc
end

#end_keyword_locObject (readonly)

attr_reader end_keyword_loc: Location



12665
12666
12667
# File 'lib/prism/node.rb', line 12665

def end_keyword_loc
  @end_keyword_loc
end

#expressionObject (readonly)

attr_reader expression: Node



12659
12660
12661
# File 'lib/prism/node.rb', line 12659

def expression
  @expression
end

#localsObject (readonly)

attr_reader locals: Array



12650
12651
12652
# File 'lib/prism/node.rb', line 12650

def locals
  @locals
end

#operator_locObject (readonly)

attr_reader operator_loc: Location



12656
12657
12658
# File 'lib/prism/node.rb', line 12656

def operator_loc
  @operator_loc
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



12679
12680
12681
# File 'lib/prism/node.rb', line 12679

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

#child_nodesObject Also known as: deconstruct

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



12684
12685
12686
# File 'lib/prism/node.rb', line 12684

def child_nodes
  [expression, body]
end

#class_keywordObject

def class_keyword: () -> String



12723
12724
12725
# File 'lib/prism/node.rb', line 12723

def class_keyword
  class_keyword_loc.slice
end

#comment_targetsObject

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



12697
12698
12699
# File 'lib/prism/node.rb', line 12697

def comment_targets
  [class_keyword_loc, operator_loc, expression, *body, end_keyword_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



12689
12690
12691
12692
12693
12694
# File 'lib/prism/node.rb', line 12689

def compact_child_nodes
  compact = []
  compact << expression
  compact << body if body
  compact
end

#copy(**params) ⇒ Object

def copy: (**params) -> SingletonClassNode



12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
# File 'lib/prism/node.rb', line 12702

def copy(**params)
  SingletonClassNode.new(
    params.fetch(:locals) { locals },
    params.fetch(:class_keyword_loc) { class_keyword_loc },
    params.fetch(:operator_loc) { operator_loc },
    params.fetch(:expression) { expression },
    params.fetch(:body) { body },
    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[Node] | String | Token | Array[Token] | Location]



12718
12719
12720
# File 'lib/prism/node.rb', line 12718

def deconstruct_keys(keys)
  { locals: locals, class_keyword_loc: class_keyword_loc, operator_loc: operator_loc, expression: expression, body: body, end_keyword_loc: end_keyword_loc, location: location }
end

#end_keywordObject

def end_keyword: () -> String



12733
12734
12735
# File 'lib/prism/node.rb', line 12733

def end_keyword
  end_keyword_loc.slice
end

#inspect(inspector = NodeInspector.new) ⇒ Object



12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
# File 'lib/prism/node.rb', line 12737

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  inspector << "├── locals: #{locals.inspect}\n"
  inspector << "├── class_keyword_loc: #{inspector.location(class_keyword_loc)}\n"
  inspector << "├── operator_loc: #{inspector.location(operator_loc)}\n"
  inspector << "├── expression:\n"
  inspector << inspector.child_node(expression, "")
  if (body = self.body).nil?
    inspector << "├── body: ∅\n"
  else
    inspector << "├── body:\n"
    inspector << body.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  inspector << "└── end_keyword_loc: #{inspector.location(end_keyword_loc)}\n"
  inspector.to_str
end

#operatorObject

def operator: () -> String



12728
12729
12730
# File 'lib/prism/node.rb', line 12728

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



12768
12769
12770
# File 'lib/prism/node.rb', line 12768

def type
  :singleton_class_node
end