Class: Lisp::Format::Directives::Radix

Inherits:
Directive show all
Defined in:
lib/carat/lisp-format.rb

Overview

Represents the ~R (Radix) directive. This outputs numbers in a given radix, or using alternative forms, such as Roman numerals or cardinal/ordinal English numbers.

Constant Summary collapse

@@names =
{
  1 => 'one',      2 => 'two',   3 => 'three',
  4 => 'four',       5 => 'five',   6 => 'six',
  7 => 'seven',     8 => 'eight',  9 => 'nine',
  10 => 'ten',      11 => 'eleven',  12 => 'twelve',
  13 => 'thirteen', 14 => 'fourteen', 15 => 'fifteen',
  16 => 'sixteen',  17 => 'seventeen',  18 => 'eighteen',
  19 => 'nineteen', 20 => 'twenty', 30 => 'thirty',
  40 => 'forty',    50 => 'fifty',  60 => 'sixty',
  70 => 'seventy',  80 => 'eighty', 90 => 'ninety'
}
@@illions =
%w[ \ 
  thousand  million    billion     trillion
  quadrillion   quintillion sextillion    septillion
  octillion nonillion decillion   undecillion
  duodecillion  tredecillion  quattuordecillion quindecillion
  sexdecillion  septendecillion octodecillion     novemdecillion
  vigintillion
]
@@ordinal_ones =
%w[
  \     first     second      third       fourth
  fifth     sixth     seventh     eight       ninth
  tenth     eleventh  twelfth     thirteenth  fourteenth
  fifteenth sixteenth seventeenth eighteenth  nineteenth
]
@@ordinal_tens =
%w[
  \     \       twentieth   thirtieth fortieth
  fiftieth  sixtieth  seventieth  eightieth ninetieth
]
@@romans =
[
  [1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'], [100, 'C'],
  [90,  'XC'], [50,   'L'], [40, 'XL'], [10,   'X'], [9,  'IX'],
  [5,    'V'], [4,   'IV'], [1,   'I']
]
@@old_romans =
[
  [1000,  'M'], [900, 'DCCCC'], [500, 'D'], [400, 'CCCC'],
  [100,   'C'], [90,  'LXXXX'], [50,  'L'], [40,  'XXXX'],
  [10,    'X'], [9,   'VIIII'], [5,   'V'], [4,   'IIII'],
  [1,   'I']
]

Instance Attribute Summary

Attributes inherited from Directive

#pos

Instance Method Summary collapse

Methods inherited from Directive

#initialize, #join

Constructor Details

This class inherits a constructor from Lisp::Format::Directives::Directive

Instance Method Details

#execute(state) ⇒ Object

Output the given argument using one of a variety of methods. Either the argument is output using a specific radix, using cardinal or ordinal English numbers, or Roman numerals. The full form is

~radix,mincol,padchar,commachar,commainterval:@R

with the same interpretation as for ~D (Decimal / Number), but using the given radix instead. An ArgumentError is raised if the argument is not an integer, or, in the case of Roman numerals, if the argument is not a Fixnum value.

If none of the parameters are given the output instead depends on the combination of modifiers:

no modifiers

the number is output as a cardinal English number,

:

the number is output as an ordinal English number,

@

the number is output as a Roman numeral,

:@

the number is output as an old Roman numeral.

An ArgumentError is raised if the argument is not an integer.



784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
# File 'lib/carat/lisp-format.rb', line 784

def execute(state)
  if @params.size > 0
    # XXX: may be ugly to modify these
    n = @params.shift.value
    Factory.build(@params, @modifiers, nil, @pos, n).execute(state)
  else
    arg = state.next_arg
    if arg.respond_to? :to_int
      conversion = :cardinal
      conversion = :ordinal if colon_mod?
      conversion = :roman if at_mod?
      conversion = :old_roman if colon_mod? and at_mod?
      state.output self.send(conversion, arg.to_int)
    else
      arg_error 'argument is not an integer'
    end
  end
end