From e86bc6bb478f5a73b960d4b08e102c7139eae2e1 Mon Sep 17 00:00:00 2001 From: Akira Moroo Date: Sat, 30 Jan 2016 14:25:53 +0900 Subject: Add String#rjust to mruby-string-ext --- mrbgems/mruby-string-ext/mrblib/string.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) 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 integer is greater than the length of str, returns a new + # String of length integer with str right justified + # and padded with padstr; otherwise, returns str. + # + # "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 # -- cgit v1.2.3 From cb1a80e74195f92ef6eca801d1e92a93788987db Mon Sep 17 00:00:00 2001 From: Akira Moroo Date: Sat, 30 Jan 2016 14:28:00 +0900 Subject: Add String#rjust test to mruby-string-ext --- mrbgems/mruby-string-ext/test/string.rb | 7 +++++++ 1 file changed, 7 insertions(+) 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" -- cgit v1.2.3