Skip to content

Commit

Permalink
Add String#rjust to mruby-string-ext
Browse files Browse the repository at this point in the history
  • Loading branch information
retrage committed Jan 30, 2016
1 parent c35a25c commit e86bc6b
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions mrbgems/mruby-string-ext/mrblib/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,29 @@ def ljust(idx, padstr = ' ')
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
#
Expand Down

0 comments on commit e86bc6b

Please sign in to comment.