Method: FormatR::Format#initialize
- Defined in:
- lib/formatr.rb
#initialize(mid_or_top, mid_format = nil, bottom_format = nil) ⇒ Format
Create a new format with the given top, bottom, and middle formats. One argument will default to a top while two will give you a top and a middle. If you want a bottom and no top you’ll need to pass an empty format in as the first argument or a nil. The output defaults to standard out but can be changed with the Format.io= method.
The format is a string in the style of a perl format or an array of strings each of which is a line of a perl format. The passed in format contains multiple lines, picture lines and argument lines. A picture line can contain any text but if it contains an at field (@ followed by any number of <,>,| or a group of #‘s of the format #.# or #*) it must be followed by an argument line. The arguments in the argument line are inserted in place of the at fields in the picture line. Perl documentation for formats can be found here: www.cpan.org/doc/manual/html/pod/perlform.html An example of a format is
format = "Name: @<<< @<<<<<\n first_name last_name\n"
This line specifies that when requested one line should be printed and that it will say “Name: #first_name #last_namen” but that if either of those variables is longer than the length its format the result will be truncated to the length of the format.
An at field specified as @<* specifies that the variable should be left justified within the space allocated. @>* is right justified, and @| is centered. #‘s are used to print numbers and can be used to set the number of digits after the decimal point. However the whole number portion of an argument will always be printed in its entirety even if it takes space set for the fractional portion or even more space. If the fractional portion is not long enough to fill the described space it will be padded with 0s.
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 |
# File 'lib/formatr.rb', line 828 def initialize (mid_or_top, mid_format=nil, bottom_format=nil) if (mid_or_top.nil?) raise FormatException.new(), " You need to pass in at least one non-nil argument" end @use_hash = false @io = $stdout @picture_lines = [] @vars = [] @top = @bottom = nil @page_length = 60 @lines_left = @page_length @format_length = 0 @buffered_lines = [] @print_top = true @printed_a_body = false @print_bottom = false @page_number = 1 lines = ((mid_format.nil?) ? mid_or_top : mid_format) if (lines.class == String) lines = lines.split( /\n/ ) end expected_vars = 0 lines.each {|line| if (line =~ /^#.*/) #don't do anything, it's a comment elsif (0 != expected_vars) expected_vars = getVarLine(line, expected_vars) else expected_vars = getPictureLine(line, expected_vars) @format_length += 1 end } setTop(mid_or_top) if mid_format setBottom(bottom_format) if bottom_format end |