Commit ce84e42f authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto

Merge pull request #3093 from retrage/retrage/dev

Add String#rjust to mruby-string-ext
parents ac564970 cb1a80e7
......@@ -287,6 +287,29 @@ class String
return newstr.slice(0,idx)
end
##
# call-seq:
# str.rjust(integer, padstr=' ') -> new_str
#
# If <i>integer</i> is greater than the length of <i>str</i>, returns a new
# <code>String</code> of length <i>integer</i> with <i>str</i> right justified
# and padded with <i>padstr</i>; otherwise, returns <i>str</i>.
#
# "hello".rjust(4) #=> "hello"
# "hello".rjust(20) #=> " hello"
# "hello".rjust(20, '1234') #=> "123412341234123hello"
def rjust(idx, padstr = ' ')
if idx <= self.size
return self
end
padsize = idx - self.size
newstr = padstr.dup
while newstr.size <= padsize
newstr << padstr
end
return newstr.slice(0,padsize) + self
end
# str.upto(other_str, exclusive=false) {|s| block } -> str
# str.upto(other_str, exclusive=false) -> an_enumerator
#
......
......@@ -421,6 +421,13 @@ assert('String#ljust') do
assert_equal "hello", "hello".ljust(-3)
end
assert('String#rjust') do
assert_equal "hello", "hello".rjust(4)
assert_equal " hello", "hello".rjust(20)
assert_equal "123412341234123hello", "hello".rjust(20, '1234')
assert_equal "hello", "hello".rjust(-3)
end
assert('String#upto') do
a = "aa"
start = "aa"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment