summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-string-ext
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2021-04-28 21:41:22 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2021-04-28 21:44:19 +0900
commit2ad5d4f53df27e40dac1d83a20317360088f6493 (patch)
tree7762d81989ea41e2cf4c46e1bd31d707acf9f404 /mrbgems/mruby-string-ext
parente5e5acefaf922243aef707d8eff77f7e22db03fa (diff)
downloadmruby-2ad5d4f53df27e40dac1d83a20317360088f6493.tar.gz
mruby-2ad5d4f53df27e40dac1d83a20317360088f6493.zip
string.c: add a new method `String#center`.
Diffstat (limited to 'mrbgems/mruby-string-ext')
-rw-r--r--mrbgems/mruby-string-ext/mrblib/string.rb28
-rw-r--r--mrbgems/mruby-string-ext/test/string.rb8
2 files changed, 32 insertions, 4 deletions
diff --git a/mrbgems/mruby-string-ext/mrblib/string.rb b/mrbgems/mruby-string-ext/mrblib/string.rb
index c61f427dc..ff113dd95 100644
--- a/mrbgems/mruby-string-ext/mrblib/string.rb
+++ b/mrbgems/mruby-string-ext/mrblib/string.rb
@@ -275,8 +275,8 @@ class String
def ljust(idx, padstr = ' ')
raise ArgumentError, 'zero width padding' if padstr == ''
return self if idx <= self.size
- pad_repetitions = (idx / padstr.length).ceil
- padding = (padstr * pad_repetitions)[0...(idx - self.length)]
+ pad_repetitions = idx / padstr.size
+ padding = (padstr * pad_repetitions)[0, idx-self.size]
self + padding
end
@@ -294,11 +294,31 @@ class String
def rjust(idx, padstr = ' ')
raise ArgumentError, 'zero width padding' if padstr == ''
return self if idx <= self.size
- pad_repetitions = (idx / padstr.length).ceil
- padding = (padstr * pad_repetitions)[0...(idx - self.length)]
+ pad_repetitions = idx / padstr.size
+ padding = (padstr * pad_repetitions)[0, idx-self.size]
padding + self
end
+ ##
+ # call-seq:
+ # str.center(width, padstr=' ') -> new_str
+ #
+ # Centers +str+ in +width+. If +width+ is greater than the length of +str+,
+ # returns a new String of length +width+ with +str+ centered and padded with
+ # +padstr+; otherwise, returns +str+.
+ #
+ # "hello".center(4) #=> "hello"
+ # "hello".center(20) #=> " hello "
+ # "hello".center(20, '123') #=> "1231231hello12312312"
+ def center(width, padstr = ' ')
+ raise ArgumentError, 'zero width padding' if padstr == ''
+ return self if width <= self.size
+ width -= self.size
+ pad1 = width / 2
+ pad2 = width - pad1
+ (padstr*pad1)[0,pad1] + self + (padstr*pad2)[0,pad2]
+ end
+
def chars(&block)
if block_given?
self.split('').each do |i|
diff --git a/mrbgems/mruby-string-ext/test/string.rb b/mrbgems/mruby-string-ext/test/string.rb
index 3f11c00a0..7be673aa6 100644
--- a/mrbgems/mruby-string-ext/test/string.rb
+++ b/mrbgems/mruby-string-ext/test/string.rb
@@ -474,6 +474,14 @@ assert('String#rjust') do
assert_equal "hello", "hello".rjust(-3)
end
+assert('String#center') do
+ assert_equal "hello", "hello".center(4)
+ assert_equal " hello ", "hello".center(20)
+ assert_equal 20, "hello".center(20).length
+ assert_equal "1231231hello12312312", "hello".center(20, '123')
+ assert_equal "hello", "hello".center(-3)
+end
+
if UTF8STRING
assert('String#ljust with UTF8') do
assert_equal "helloん ", "helloん".ljust(20)