Class: Lisp::Format::Directives::Print
- Defined in:
- lib/carat/lisp-format.rb
Overview
Super-class for ‘printing’ directives, namely ~A (Ascii) and ~S (SExpression). These directives print, in some sense, their argument in a Ruby friendly manner. This means that their argument is either converted to a string using Object#to_s or Object#inspect.
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Directive
Instance Method Summary collapse
-
#execute(state) ⇒ Object
Output the given argument as it generally prints in Ruby.
-
#initialize(params, modifiers, top, pos, inspect = false) ⇒ Print
constructor
All parameters except
inspect
are simply passed on to Directive#initialize.
Methods inherited from Directive
Constructor Details
#initialize(params, modifiers, top, pos, inspect = false) ⇒ Print
All parameters except inspect
are simply passed on to Directive#initialize. If inspect
is true, string arguments are inspected as well as all other objects.
584 585 586 587 |
# File 'lib/carat/lisp-format.rb', line 584 def initialize(params, modifiers, top, pos, inspect = false) super params, modifiers, top, pos @inspect = inspect end |
Instance Method Details
#execute(state) ⇒ Object
Output the given argument as it generally prints in Ruby. The full form is:
~mincol,colinc,minpad,padchar:@[AS]
with the following interpretations
mincol
(0)-
minimum number of columns to output,
colinc
(1)-
number of columns to increase by, until
mincol
is reached, minpad
(0)-
minimum amount of padding to add (added before
mincol
is checked), padchar
(?s)-
character to pad with,
- :
-
nil
is output asnil
(In Lisp, this outputsnil
as()
, which isn’t generally useful in Ruby. TODO: come up with better use for this modifier), - @
-
padding is done on the right.
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 |
# File 'lib/carat/lisp-format.rb', line 608 def execute(state) padmethod = at_mod? ? :rjust : :ljust mincol = param(0, state, 0) colinc = param(1, state, 1) minpad = param(2, state, 0) padchar = param(3, state, ?\s).chr arg = state.next_arg # XXX: this needs checking use .to_s here? str = (arg.is_a? String and not @inspect) ? arg.to_s : arg.inspect str = str.send(padmethod, str.length + minpad, padchar) k = ((mincol - str.length) / colinc.to_f).ceil if k > 0 str = str.send(padmethod, str.length + colinc * k, padchar) end state.output str end |