Module: Flippy::Vertical

Included in:
Flippy
Defined in:
lib/flippy/vertical.rb

Constant Summary collapse

PUNC =
%w(   )
HV =
(h + v)
VH =
(v + h)
SEPARATOR =
'++'

Instance Method Summary collapse

Instance Method Details

#verticalObject

Convert to left-right vertical writing style.

"Hi\nFo".vertical => "F H "
                     "o i "

Last part after “++” symbol is taken as a normal writing part.

"Hi\nFo++by me".vertical => "F H "
                            "o i by me"


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/flippy/vertical.rb', line 20

def vertical
  lines = []
  v_part, _, h_part = self.rpartition(SEPARATOR).reject(&:empty?)
  v_part.to_s.tr(HV, VH)
      .each_line do |line|
        main_line, punc_line = [], []
        line.chomp.each_char.with_index do |chr, i|
          if i.zero? || !PUNC.include?(chr)
            main_line << chr
            punc_line << ' '
          else
            if PUNC.include?(punc_line.last)
              main_line << ' '
              punc_line << chr
            else
              punc_line[-1] = chr
            end
          end
        end
        lines << punc_line << main_line
      end

  max = lines.map(&:size).max
  lines.map { |line| line.fill(' ', line.size...max) }
       .transpose
       .map { |line| line.join.reverse }.join("\n")
       .gsub(/ +$/, '').concat("#{h_part}")
end