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
515a0932
Commit
515a0932
authored
Jul 15, 2017
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add `Hash#transform_keys!` and `Hash#transform_values`.
parent
d7fef24a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
0 deletions
+41
-0
mrbgems/mruby-hash-ext/mrblib/hash.rb
mrbgems/mruby-hash-ext/mrblib/hash.rb
+37
-0
mrbgems/mruby-hash-ext/test/hash.rb
mrbgems/mruby-hash-ext/test/hash.rb
+4
-0
No files found.
mrbgems/mruby-hash-ext/mrblib/hash.rb
View file @
515a0932
...
...
@@ -404,6 +404,26 @@ class Hash
end
##
# call-seq:
# hsh.transform_keys! {|key| block } -> hsh
# hsh.transform_keys! -> an_enumerator
#
# Invokes the given block once for each key in <i>hsh</i>, replacing it
# with the new key returned by the block, and then returns <i>hsh</i>.
#
# If no block is given, an enumerator is returned instead.
#
def
transform_keys!
(
&
b
)
return
to_enum
:transform_keys!
unless
block_given?
self
.
keys
.
each
do
|
k
|
value
=
self
[
k
]
new_key
=
yield
(
k
)
self
.
__delete
(
k
)
self
[
new_key
]
=
value
end
self
end
##
# call-seq:
# hsh.transform_values {|value| block } -> new_hash
# hsh.transform_values -> an_enumerator
#
...
...
@@ -421,4 +441,21 @@ class Hash
end
hash
end
##
# call-seq:
# hsh.transform_values! {|key| block } -> hsh
# hsh.transform_values! -> an_enumerator
#
# Invokes the given block once for each value in the hash, replacing
# with the new value returned by the block, and then returns <i>hsh</i>.
#
# If no block is given, an enumerator is returned instead.
#
def
transform_values!
(
&
b
)
return
to_enum
:transform_values!
unless
block_given?
self
.
keys
.
each
do
|
k
|
self
[
k
]
=
yield
(
self
[
k
])
end
self
end
end
mrbgems/mruby-hash-ext/test/hash.rb
View file @
515a0932
...
...
@@ -263,6 +263,8 @@ assert("Hash#transform_keys") do
{
1
=>
100
,
2
=>
200
})
assert_equal
(
h
.
transform_keys
.
with_index
{
|
k
,
i
|
"
#{
k
}
.
#{
i
}
"
},
{
"1.0"
=>
100
,
"2.1"
=>
200
})
assert_equal
(
h
.
transform_keys!
{
|
k
|
k
.
to_i
},
h
)
assert_equal
(
h
,
{
1
=>
100
,
2
=>
200
})
end
assert
(
"Hash#transform_values"
)
do
...
...
@@ -273,4 +275,6 @@ assert("Hash#transform_values") do
{
a:
"1"
,
b:
"2"
,
c:
"3"
})
assert_equal
(
h
.
transform_values
.
with_index
{
|
v
,
i
|
"
#{
v
}
.
#{
i
}
"
},
{
a:
"1.0"
,
b:
"2.1"
,
c:
"3.2"
})
assert_equal
(
h
.
transform_values!
{
|
v
|
v
.
to_s
},
h
)
assert_equal
(
h
,
{
a:
"1"
,
b:
"2"
,
c:
"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