Module: CommentBoxStringExtensions

Defined in:
lib/commentbox.rb

Instance Method Summary collapse

Instance Method Details

#align_to(side, length) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/commentbox.rb', line 67

def align_to (side, length)
  if side == :left
    self.justify_to length
  elsif side == :right
    self.right_justify_to length
  elsif side == :center
    self.center_align length
  else
    raise 'String#align_to : expected :left, :right, or :center here'
  end
end

#center_align(length) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/commentbox.rb', line 60

def center_align (length)
  if !length.is_even? then raise 'String#center_align : length must be even' end
  llength = ((length - self.length) / 2)
  rlength = llength
  if !self.length.is_even? then rlength += 1 end
  (' ' * llength) + self + (' ' * rlength)
end

#justify_to(length) ⇒ Object



54
55
56
# File 'lib/commentbox.rb', line 54

def justify_to (length)
  self << ' ' * (length - self.length)
end

#right_justify_to(length) ⇒ Object



57
58
59
# File 'lib/commentbox.rb', line 57

def right_justify_to (length)
  (' ' * (length - self.length)) + self
end