class NumberMuncher::Token::Base

Attributes

captures[R]
text[R]
value[R]

Public Class Methods

captures(scanner, text) click to toggle source
# File lib/number_muncher/token/base.rb, line 11
def self.captures(scanner, text)
  if scanner.respond_to?(:captures)
    scanner.captures.map(&:presence)
  else
    match = regex.match(text)

    match.regexp.named_captures.each_with_object([]) do |(capture, _), arr|
      arr << match[capture]
    end
  end
end
new(text, captures = nil) click to toggle source
# File lib/number_muncher/token/base.rb, line 23
def initialize(text, captures = nil)
  @text = text
  @captures = captures
  @value = Numeric.new(parse)
end
scan(scanner) click to toggle source
# File lib/number_muncher/token/base.rb, line 7
def self.scan(scanner)
  new(scanner.matched, captures(scanner, scanner.matched)) if scanner.scan(regex)
end

Public Instance Methods

==(other) click to toggle source
Calls superclass method
# File lib/number_muncher/token/base.rb, line 48
def ==(other)
  return to_a == other if other.is_a?(Array)

  super
end
float?() click to toggle source
# File lib/number_muncher/token/base.rb, line 33
def float?
  false
end
fraction?() click to toggle source
# File lib/number_muncher/token/base.rb, line 37
def fraction?
  false
end
inspect() click to toggle source
# File lib/number_muncher/token/base.rb, line 54
def inspect
  to_a.to_s
end
int?() click to toggle source
# File lib/number_muncher/token/base.rb, line 29
def int?
  false
end
to_a() click to toggle source
# File lib/number_muncher/token/base.rb, line 41
def to_a
  [
    self.class.name.demodulize.underscore.to_sym,
    value
  ]
end

Private Instance Methods

parse() click to toggle source
# File lib/number_muncher/token/base.rb, line 60
def parse
  Rational(text)
end