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
698b3c92
Commit
698b3c92
authored
Sep 10, 2015
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add Hash#rehash to handle key modification; ref #2945
parent
543ac88e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
0 deletions
+32
-0
mrblib/hash.rb
mrblib/hash.rb
+23
-0
test/t/hash.rb
test/t/hash.rb
+9
-0
No files found.
mrblib/hash.rb
View file @
698b3c92
...
...
@@ -319,6 +319,29 @@ class Hash
h
end
##
# call-seq:
# hsh.rehash -> hsh
#
# Rebuilds the hash based on the current hash values for each key. If
# values of key objects have changed since they were inserted, this
# method will reindex <i>hsh</i>.
#
# h = {"AAA" => "b"}
# h.keys[0].chop!
# h #=> {"AA"=>"b"}
# h["AA"] #=> nil
# h.rehash #=> {"AA"=>"b"}
# h["AA"] #=> "b"
#
def
rehash
h
=
{}
self
.
each
{
|
k
,
v
|
h
[
k
]
=
v
}
self
.
replace
(
h
)
end
def
__update
(
h
)
h
.
each_key
{
|
k
|
self
[
k
]
=
h
[
k
]}
self
...
...
test/t/hash.rb
View file @
698b3c92
...
...
@@ -342,3 +342,12 @@ assert('Hash#inspect') do
assert_include
ret
,
'"a"=>100'
assert_include
ret
,
'"d"=>400'
end
assert
(
'Hash#rehash'
)
do
h
=
{[
:a
]
=>
"b"
}
# hash key modified
h
.
keys
[
0
][
0
]
=
:b
# h[[:b]] => nil
h
.
rehash
assert_equal
(
"b"
,
h
[[
:b
]])
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