diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2015-01-13 16:19:46 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2015-01-13 16:19:46 +0900 |
| commit | 4aef1ad209179e67bbeb9d3bfaad2f5d233d958e (patch) | |
| tree | 366cf6146d52f93e4c72a96e0b632949222fd463 /mrbgems/mruby-string-ext/mrblib/string.rb | |
| parent | 1ab79ac41494320624cbfd40163ebcf4634caf31 (diff) | |
| parent | 1b606025ed1a9813267bf8ac4440fc9dd5fc120c (diff) | |
| download | mruby-4aef1ad209179e67bbeb9d3bfaad2f5d233d958e.tar.gz mruby-4aef1ad209179e67bbeb9d3bfaad2f5d233d958e.zip | |
Merge pull request #2700 from takahashim/string-ljust
add String#ljust into mruby-string-ext
Diffstat (limited to 'mrbgems/mruby-string-ext/mrblib/string.rb')
| -rw-r--r-- | mrbgems/mruby-string-ext/mrblib/string.rb | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/mrbgems/mruby-string-ext/mrblib/string.rb b/mrbgems/mruby-string-ext/mrblib/string.rb index 88bdd9090..fd805b144 100644 --- a/mrbgems/mruby-string-ext/mrblib/string.rb +++ b/mrbgems/mruby-string-ext/mrblib/string.rb @@ -244,4 +244,27 @@ class String return str + self if pos == 0 return self[0..pos - 1] + str + self[pos..-1] end + + ## + # call-seq: + # str.ljust(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> left justified + # and padded with <i>padstr</i>; otherwise, returns <i>str</i>. + # + # "hello".ljust(4) #=> "hello" + # "hello".ljust(20) #=> "hello " + # "hello".ljust(20, '1234') #=> "hello123412341234123" + def ljust(idx, padstr = ' ') + if idx <= self.size + return self + end + newstr = self.dup + newstr << padstr + while newstr.size <= idx + newstr << padstr + end + return newstr.slice(0,idx) + end end |
