class String

String

Public Instance Methods

surround(options = { top_bottom: '-', side: '|' }) click to toggle source

surround string.

Options

  • :top_bottom - set top and bottom charactor

  • :side - set right and left charactor

Examples

single line, no option case

'hoge'.surround

result

------
|hoge|
------

multi line, no option case

"hoge\na".surround

result

------
|hoge|
|a   |
------

single line, both option case

'hoge'.surround top_bottom: '=', side: '!'

result

======
!hoge!
======
# File lib/pry-aa_ancestors.rb, line 45
def surround(options = { top_bottom: '-', side: '|' })
  top_bottom = init_top_bottom(options)
  side = init_side(options)
  inner_width = line_max
  top_bottom = top_bottoms(top_bottom, inner_width)
  ret = *each_line.reduce([top_bottom]) do |a, e|
    a << "#{side}#{e.chomp.ljust(inner_width)}#{side}"
  end
  ret.push(top_bottom).join("\n")
end

Private Instance Methods

init_side(options) click to toggle source
# File lib/pry-aa_ancestors.rb, line 62
def init_side(options)
  options[:side].nil? ? '|' : options[:side]
end
init_top_bottom(options) click to toggle source
# File lib/pry-aa_ancestors.rb, line 58
def init_top_bottom(options)
  options[:top_bottom].nil? ? '-' : options[:top_bottom]
end
line_max() click to toggle source
# File lib/pry-aa_ancestors.rb, line 70
def line_max
  return 0 if self.empty?
  each_line.max_by(&:size).chomp.size
end
top_bottoms(top_bottom, inner_width) click to toggle source
# File lib/pry-aa_ancestors.rb, line 66
def top_bottoms(top_bottom, inner_width)
  top_bottom * (inner_width + 2)
end