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
ff765c81
Unverified
Commit
ff765c81
authored
Apr 20, 2019
by
Yukihiro "Matz" Matsumoto
Committed by
GitHub
Apr 20, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4390 from shuujii/add-type-check-in-String-aset
Add type check (conversion) in `String#[]=`
parents
2f9ebc6f
4a8b88f7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
13 additions
and
5 deletions
+13
-5
mrblib/string.rb
mrblib/string.rb
+6
-5
test/t/string.rb
test/t/string.rb
+7
-0
No files found.
mrblib/string.rb
View file @
ff765c81
...
...
@@ -197,12 +197,12 @@ class String
def
[]=
(
*
args
)
anum
=
args
.
size
if
anum
==
2
pos
,
value
=
args
pos
,
value
=
args
[
0
],
args
[
1
].
__to_str
case
pos
when
String
posnum
=
self
.
index
(
pos
)
if
posnum
b
=
self
[
0
,
posnum
.
to_i
]
b
=
self
[
0
,
posnum
]
a
=
self
[(
posnum
+
pos
.
length
)
..-
1
]
self
.
replace
([
b
,
value
,
a
].
join
(
''
))
else
...
...
@@ -217,17 +217,18 @@ class String
end
return
self
[
head
,
tail
-
head
]
=
value
else
pos
=
pos
.
__to_int
pos
+=
self
.
length
if
pos
<
0
if
pos
<
0
||
pos
>
self
.
length
raise
IndexError
,
"index
#{
args
[
0
]
}
out of string"
end
b
=
self
[
0
,
pos
.
to_i
]
b
=
self
[
0
,
pos
]
a
=
self
[
pos
+
1
..-
1
]
self
.
replace
([
b
,
value
,
a
].
join
(
''
))
end
return
value
elsif
anum
==
3
pos
,
len
,
value
=
args
pos
,
len
,
value
=
args
[
0
].
__to_int
,
args
[
1
].
__to_int
,
args
[
2
].
__to_str
pos
+=
self
.
length
if
pos
<
0
if
pos
<
0
||
pos
>
self
.
length
raise
IndexError
,
"index
#{
args
[
0
]
}
out of string"
...
...
@@ -235,7 +236,7 @@ class String
if
len
<
0
raise
IndexError
,
"negative length
#{
len
}
"
end
b
=
self
[
0
,
pos
.
to_i
]
b
=
self
[
0
,
pos
]
a
=
self
[
pos
+
len
..-
1
]
self
.
replace
([
b
,
value
,
a
].
join
(
''
))
return
value
...
...
test/t/string.rb
View file @
ff765c81
...
...
@@ -161,6 +161,9 @@ assert('String#[]=') do
assert_equal
'aXc'
,
e
end
assert_raise
(
TypeError
)
{
'a'
[
0
]
=
1
}
assert_raise
(
TypeError
)
{
'a'
[
:a
]
=
'1'
}
# length of args is 2
a1
=
'abc'
assert_raise
(
IndexError
)
do
...
...
@@ -197,6 +200,10 @@ assert('String#[]=') do
assert_raise
(
IndexError
)
do
b3
[
'XX'
]
=
'Y'
end
assert_raise
(
TypeError
)
{
'a'
[
:a
,
0
]
=
'1'
}
assert_raise
(
TypeError
)
{
'a'
[
0
,
:a
]
=
'1'
}
assert_raise
(
TypeError
)
{
'a'
[
0
,
1
]
=
1
}
end
assert
(
'String#capitalize'
,
'15.2.10.5.7'
)
do
...
...
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