Class: Prism::RationalNode

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

Overview

Represents a rational number literal.

1.0r
^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, numerator, denominator) ⇒ RationalNode

Initialize a new RationalNode node.



15074
15075
15076
15077
15078
15079
15080
15081
# File 'lib/prism/node.rb', line 15074

def initialize(source, node_id, location, flags, numerator, denominator)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @numerator = numerator
  @denominator = denominator
end

Instance Attribute Details

#denominatorObject (readonly)

The denominator of the rational number.

1.5r # denominator 2


15144
15145
15146
# File 'lib/prism/node.rb', line 15144

def denominator
  @denominator
end

#numeratorObject (readonly)

The numerator of the rational number.

1.5r # numerator 3


15139
15140
15141
# File 'lib/prism/node.rb', line 15139

def numerator
  @numerator
end

Class Method Details

.typeObject

Return a symbol representation of this node type. See ‘Node::type`.



15157
15158
15159
# File 'lib/prism/node.rb', line 15157

def self.type
  :rational_node
end

Instance Method Details

#===(other) ⇒ Object

Implements case-equality for the node. This is effectively == but without comparing the value of locations. Locations are checked only for presence.



15163
15164
15165
15166
15167
15168
# File 'lib/prism/node.rb', line 15163

def ===(other)
  other.is_a?(RationalNode) &&
    (flags === other.flags) &&
    (numerator === other.numerator) &&
    (denominator === other.denominator)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



15084
15085
15086
# File 'lib/prism/node.rb', line 15084

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

#binary?Boolean

def binary?: () -> bool

Returns:

  • (Boolean)


15117
15118
15119
# File 'lib/prism/node.rb', line 15117

def binary?
  flags.anybits?(IntegerBaseFlags::BINARY)
end

#child_nodesObject Also known as: deconstruct

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



15089
15090
15091
# File 'lib/prism/node.rb', line 15089

def child_nodes
  []
end

#comment_targetsObject

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



15099
15100
15101
# File 'lib/prism/node.rb', line 15099

def comment_targets
  [] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



15094
15095
15096
# File 'lib/prism/node.rb', line 15094

def compact_child_nodes
  []
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, numerator: self.numerator, denominator: self.denominator) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?numerator: Integer, ?denominator: Integer) -> RationalNode



15104
15105
15106
# File 'lib/prism/node.rb', line 15104

def copy(node_id: self.node_id, location: self.location, flags: self.flags, numerator: self.numerator, denominator: self.denominator)
  RationalNode.new(source, node_id, location, flags, numerator, denominator)
end

#decimal?Boolean

def decimal?: () -> bool

Returns:

  • (Boolean)


15122
15123
15124
# File 'lib/prism/node.rb', line 15122

def decimal?
  flags.anybits?(IntegerBaseFlags::DECIMAL)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, numerator: Integer, denominator: Integer }



15112
15113
15114
# File 'lib/prism/node.rb', line 15112

def deconstruct_keys(keys)
  { node_id: node_id, location: location, numerator: numerator, denominator: denominator }
end

#hexadecimal?Boolean

def hexadecimal?: () -> bool

Returns:

  • (Boolean)


15132
15133
15134
# File 'lib/prism/node.rb', line 15132

def hexadecimal?
  flags.anybits?(IntegerBaseFlags::HEXADECIMAL)
end

#inspectObject

def inspect -> String



15147
15148
15149
# File 'lib/prism/node.rb', line 15147

def inspect
  InspectVisitor.compose(self)
end

#numericObject

Returns the value of the node as an IntegerNode or a FloatNode. This method is deprecated in favor of #value or #numerator/#denominator.



120
121
122
123
124
125
126
127
128
# File 'lib/prism/node_ext.rb', line 120

def numeric
  deprecated("value", "numerator", "denominator")

  if denominator == 1
    IntegerNode.new(source, -1, location.chop, flags, numerator)
  else
    FloatNode.new(source, -1, location.chop, 0, numerator.to_f / denominator)
  end
end

#octal?Boolean

def octal?: () -> bool

Returns:

  • (Boolean)


15127
15128
15129
# File 'lib/prism/node.rb', line 15127

def octal?
  flags.anybits?(IntegerBaseFlags::OCTAL)
end

#typeObject

Return a symbol representation of this node type. See ‘Node#type`.



15152
15153
15154
# File 'lib/prism/node.rb', line 15152

def type
  :rational_node
end

#valueObject

Returns the value of the node as a Ruby Rational.



114
115
116
# File 'lib/prism/node_ext.rb', line 114

def value
  Rational(numerator, denominator)
end