Category Archives: Ruby

Gem FancyWriter

As a result of DRYing up my existing code, I created a small gem (called FancyWriter) for creating formatted plain-text files. It can indent text passages, prepend them with various comment notations (Ruby-style, C-style, etc.), and it also has basic support for formatting string-separated numeric sequences.

As an example: This code

FancyWriter::FancyIO.new(io) do
  comment '#' do
    line 'This is an example file.'
    line 'These comments explain the contents.'
  end
  line 'config:'
  indent 4 do
    line 'setting:'
    indent 4 do
      line 'some_key: some_value.'
      comment do
        line 'This is an inside comment.'
      end
    end
    line 'data:'
    indent 4 do
      write_enum [1,2,3,4]
      write_enum [5,6,7,8]
    end
  end
end

will produce the following content:

# This is an example file.
# These comments explain the contents.
config:
    setting:
        some_key: some_value.
        # This is an inside comment.
    data:
        1,2,3,4
        5,6,7,8

For more information, visit the project on GitHub.

Tagged , , , , , ,
Optimization WordPress Plugins & Solutions by W3 EDGE