Class: Prism::LambdaNode

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

Overview

Represents using a lambda literal (not the lambda method call).

->(value) { value * 2 }
^^^^^^^^^^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locals, operator_loc, opening_loc, closing_loc, parameters, body, location) ⇒ LambdaNode

def initialize: (locals: Array, operator_loc: Location, opening_loc: Location, closing_loc: Location, parameters: BlockParametersNode?, body: Node?, location: Location) -> void



8689
8690
8691
8692
8693
8694
8695
8696
8697
# File 'lib/prism/node.rb', line 8689

def initialize(locals, operator_loc, opening_loc, closing_loc, parameters, body, location)
  @locals = locals
  @operator_loc = operator_loc
  @opening_loc = opening_loc
  @closing_loc = closing_loc
  @parameters = parameters
  @body = body
  @location = location
end

Instance Attribute Details

#bodyObject (readonly)

attr_reader body: Node?



8686
8687
8688
# File 'lib/prism/node.rb', line 8686

def body
  @body
end

#closing_locObject (readonly)

attr_reader closing_loc: Location



8680
8681
8682
# File 'lib/prism/node.rb', line 8680

def closing_loc
  @closing_loc
end

#localsObject (readonly)

attr_reader locals: Array



8671
8672
8673
# File 'lib/prism/node.rb', line 8671

def locals
  @locals
end

#opening_locObject (readonly)

attr_reader opening_loc: Location



8677
8678
8679
# File 'lib/prism/node.rb', line 8677

def opening_loc
  @opening_loc
end

#operator_locObject (readonly)

attr_reader operator_loc: Location



8674
8675
8676
# File 'lib/prism/node.rb', line 8674

def operator_loc
  @operator_loc
end

#parametersObject (readonly)

attr_reader parameters: BlockParametersNode?



8683
8684
8685
# File 'lib/prism/node.rb', line 8683

def parameters
  @parameters
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



8700
8701
8702
# File 'lib/prism/node.rb', line 8700

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

#child_nodesObject Also known as: deconstruct

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



8705
8706
8707
# File 'lib/prism/node.rb', line 8705

def child_nodes
  [parameters, body]
end

#closingObject

def closing: () -> String



8754
8755
8756
# File 'lib/prism/node.rb', line 8754

def closing
  closing_loc.slice
end

#comment_targetsObject

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



8718
8719
8720
# File 'lib/prism/node.rb', line 8718

def comment_targets
  [operator_loc, opening_loc, closing_loc, *parameters, *body]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



8710
8711
8712
8713
8714
8715
# File 'lib/prism/node.rb', line 8710

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

#copy(**params) ⇒ Object

def copy: (**params) -> LambdaNode



8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
# File 'lib/prism/node.rb', line 8723

def copy(**params)
  LambdaNode.new(
    params.fetch(:locals) { locals },
    params.fetch(:operator_loc) { operator_loc },
    params.fetch(:opening_loc) { opening_loc },
    params.fetch(:closing_loc) { closing_loc },
    params.fetch(:parameters) { parameters },
    params.fetch(:body) { body },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

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



8739
8740
8741
# File 'lib/prism/node.rb', line 8739

def deconstruct_keys(keys)
  { locals: locals, operator_loc: operator_loc, opening_loc: opening_loc, closing_loc: closing_loc, parameters: parameters, body: body, location: location }
end

#inspect(inspector = NodeInspector.new) ⇒ Object



8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
# File 'lib/prism/node.rb', line 8758

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

#openingObject

def opening: () -> String



8749
8750
8751
# File 'lib/prism/node.rb', line 8749

def opening
  opening_loc.slice
end

#operatorObject

def operator: () -> String



8744
8745
8746
# File 'lib/prism/node.rb', line 8744

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



8793
8794
8795
# File 'lib/prism/node.rb', line 8793

def type
  :lambda_node
end