Class: Plurimath::Formatter::Numbers::BaseNotation

Inherits:
Object
  • Object
show all
Defined in:
lib/plurimath/formatter/numbers/base_notation.rb

Overview

Applies base prefix/postfix notation after numeric digits have already been rendered.

Constant Summary collapse

DEFAULT_PREFIXES =
{
  2 => "0b",
  8 => "0o",
  10 => "",
  16 => "0x",
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ BaseNotation

Returns a new instance of BaseNotation.



18
19
20
21
22
# File 'lib/plurimath/formatter/numbers/base_notation.rb', line 18

def initialize(options)
  @options = options
  @base = @options.base
  validate_base!
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



16
17
18
# File 'lib/plurimath/formatter/numbers/base_notation.rb', line 16

def base
  @base
end

Class Method Details

.supported?(base) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/plurimath/formatter/numbers/base_notation.rb', line 35

def self.supported?(base)
  DEFAULT_PREFIXES.key?(base)
end

Instance Method Details

#apply(string) ⇒ Object



24
25
26
27
28
29
# File 'lib/plurimath/formatter/numbers/base_notation.rb', line 24

def apply(string)
  rendered = upcase_hex? ? string.tr(Base::HEX_DIGITS, Base::HEX_DIGITS.upcase) : string
  return rendered if default?

  "#{base_prefix}#{rendered}#{base_postfix}"
end

#default?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/plurimath/formatter/numbers/base_notation.rb', line 31

def default?
  base == Base::DEFAULT_BASE
end