Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
4aef1ad2
Commit
4aef1ad2
authored
Jan 13, 2015
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2700 from takahashim/string-ljust
add String#ljust into mruby-string-ext
parents
1ab79ac4
1b606025
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
0 deletions
+30
-0
mrbgems/mruby-string-ext/mrblib/string.rb
mrbgems/mruby-string-ext/mrblib/string.rb
+23
-0
mrbgems/mruby-string-ext/test/string.rb
mrbgems/mruby-string-ext/test/string.rb
+7
-0
No files found.
mrbgems/mruby-string-ext/mrblib/string.rb
View file @
4aef1ad2
...
...
@@ -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
mrbgems/mruby-string-ext/test/string.rb
View file @
4aef1ad2
...
...
@@ -386,3 +386,10 @@ assert('String#prepend') do
assert_equal
"hello world"
,
a
.
prepend
(
"hello "
)
assert_equal
"hello world"
,
a
end
assert
(
'String#ljust'
)
do
assert_equal
"hello"
,
"hello"
.
ljust
(
4
)
assert_equal
"hello "
,
"hello"
.
ljust
(
20
)
assert_equal
"hello123412341234123"
,
"hello"
.
ljust
(
20
,
'1234'
)
assert_equal
"hello"
,
"hello"
.
ljust
(
-
3
)
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment