summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2016-01-31 11:41:01 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2016-01-31 11:41:01 +0900
commitce84e42f416660f3fc4653f11011a2146d712a94 (patch)
tree11432fdc879ba3bfebd88115f9b0f5a0a45284a0
parentac5649700c42d17346c05809ba00e089d2aa7082 (diff)
parentcb1a80e74195f92ef6eca801d1e92a93788987db (diff)
downloadmruby-ce84e42f416660f3fc4653f11011a2146d712a94.tar.gz
mruby-ce84e42f416660f3fc4653f11011a2146d712a94.zip
Merge pull request #3093 from retrage/retrage/dev
Add String#rjust to mruby-string-ext
-rw-r--r--mrbgems/mruby-string-ext/mrblib/string.rb23
-rw-r--r--mrbgems/mruby-string-ext/test/string.rb7
2 files changed, 30 insertions, 0 deletions
diff --git a/mrbgems/mruby-string-ext/mrblib/string.rb b/mrbgems/mruby-string-ext/mrblib/string.rb
index c699e13b7..3dd5561c8 100644
--- a/mrbgems/mruby-string-ext/mrblib/string.rb
+++ b/mrbgems/mruby-string-ext/mrblib/string.rb
@@ -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
#
diff --git a/mrbgems/mruby-string-ext/test/string.rb b/mrbgems/mruby-string-ext/test/string.rb
index 37a5d28cf..f68fbcb28 100644
--- a/mrbgems/mruby-string-ext/test/string.rb
+++ b/mrbgems/mruby-string-ext/test/string.rb
@@ -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"