Class: String

Inherits:
Object show all
Includes:
Comparable
Defined in:
lib/builtinME.rb

Instance Method Summary collapse

Methods included from Comparable

#<, #<=, #==, #>, #>=, #between?

Instance Method Details

#center(width, str = " ") ⇒ Object

from rubinius



620
621
622
# File 'lib/builtinME.rb', line 620

def center(width, str=" ")
    justify_string(width, str, 0)
end

#empty?Boolean

Returns:

  • (Boolean)


585
586
587
# File 'lib/builtinME.rb', line 585

def empty?
    length == 0
end

#inspectObject



589
590
591
# File 'lib/builtinME.rb', line 589

def inspect
    '"' + to_s + '"'
end

#justify_string(width, str, justify) ⇒ Object

justify left = -1, center = 0, right = 1



595
596
597
598
599
600
601
602
603
604
605
606
607
# File 'lib/builtinME.rb', line 595

def justify_string(width, str, justify)
    return self if width <= length
    pad = width - length
    out = str.to_str * (pad / str.length)
    out << str[0, pad - out.length] if out.length < pad
    # Left justification
    return self << out if justify == -1
    # Right justification
    return out << self if justify == 1
    # and finially center
    split = (width / 2) - (length / 2)
    return out.insert(split-((width-length)%2), self)
end

#ljust(width, str = " ") ⇒ Object

from rubinius



615
616
617
# File 'lib/builtinME.rb', line 615

def ljust(width, str=" ")
    justify_string(width, str, -1)
end

#rjust(width, str = " ") ⇒ Object

from rubinius



610
611
612
# File 'lib/builtinME.rb', line 610

def rjust(width, str=" ")
    justify_string(width, str, 1)
end

#to_sObject Also known as: to_str



581
582
583
# File 'lib/builtinME.rb', line 581

def to_s
    return self
end