Class: Multiline::Array

Inherits:
Array
  • Object
show all
Defined in:
lib/multiline/array.rb

Instance Method Summary collapse

Instance Method Details

#join(sep = $,, align: :center) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/multiline/array.rb', line 3

def join(sep = $,, align: :center)
  max_row = self.map{|str|
    case str
    when ::String
      str.split(/\n/).length
    when Multiline::String
      str.row
    end
  }.max

  buf = Multiline::String.new("", max_row)
  self.each_with_index do |str, index|
    case str
    when ::String
      buf.concat(Multiline::String.new(str), align: align)
    when Multiline::String
      buf.concat(str, align: align)
    end
    break if index == (self.length - 1)

    case sep
    when ::String
      buf.concat(Multiline::String.new(sep), align: align)
    when Multiline::String
      buf.concat(sep, align: align)
    end
  end
  buf
end