Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/pretty_hash/string.rb

Instance Method Summary collapse

Instance Method Details

#lpad(length, padding = " ") ⇒ Object



10
11
12
# File 'lib/pretty_hash/string.rb', line 10

def lpad(length, padding = " ")
  (padding * (length - self.print_length)) + self
end

#padding(length, type = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/pretty_hash/string.rb', line 14

def padding(length, type = nil)
  begin # auto decection code.
    raise ArgumentError if type == :string
    Float(self)
    self.lpad(length, "0")
  rescue ArgumentError
    self.rpad(length, " ")
  end
end


2
3
4
# File 'lib/pretty_hash/string.rb', line 2

def print_length
  self.each_char.map{|c| c.bytesize == 1 ? 1 : 2}.reduce(0, &:+)
end

#rpad(length, padding = " ") ⇒ Object



6
7
8
# File 'lib/pretty_hash/string.rb', line 6

def rpad(length, padding = " ")
  self + (padding * (length - self.print_length))
end