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
de3edcc1
Commit
de3edcc1
authored
Jul 14, 2017
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add `Hash#transform_{keys,values}` to `mruby-hash-ext`.
parent
bad807e7
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
68 additions
and
2 deletions
+68
-2
mrbgems/mruby-hash-ext/mrbgem.rake
mrbgems/mruby-hash-ext/mrbgem.rake
+3
-2
mrbgems/mruby-hash-ext/mrblib/hash.rb
mrbgems/mruby-hash-ext/mrblib/hash.rb
+39
-0
mrbgems/mruby-hash-ext/test/hash.rb
mrbgems/mruby-hash-ext/test/hash.rb
+26
-0
No files found.
mrbgems/mruby-hash-ext/mrbgem.rake
View file @
de3edcc1
...
...
@@ -2,6 +2,7 @@ MRuby::Gem::Specification.new('mruby-hash-ext') do |spec|
spec
.
license
=
'MIT'
spec
.
author
=
'mruby developers'
spec
.
summary
=
'Hash class extension'
spec
.
add_dependency
'mruby-enum-ext'
,
:core
=>
'mruby-enum-ext'
spec
.
add_dependency
'mruby-array-ext'
,
:core
=>
'mruby-array-ext'
spec
.
add_dependency
'mruby-enum-ext'
,
core:
'mruby-enum-ext'
spec
.
add_dependency
'mruby-array-ext'
,
core:
'mruby-array-ext'
spec
.
add_test_dependency
'mruby-enumerator'
,
core:
'mruby-enumerator'
end
mrbgems/mruby-hash-ext/mrblib/hash.rb
View file @
de3edcc1
...
...
@@ -382,4 +382,43 @@ class Hash
n
end
end
##
# call-seq:
# hsh.transform_keys {|key| block } -> new_hash
# hsh.transform_keys -> an_enumerator
#
# Returns a new hash, with the keys computed from running the block
# once for each key in the hash, and the values unchanged.
#
# If no block is given, an enumerator is returned instead.
#
def
transform_keys
(
&
b
)
return
to_enum
:transform_keys
unless
block_given?
hash
=
{}
self
.
each_key
do
|
k
|
new_key
=
yield
(
k
)
hash
[
new_key
]
=
self
[
k
]
end
hash
end
##
# call-seq:
# hsh.transform_values {|value| block } -> new_hash
# hsh.transform_values -> an_enumerator
#
# Returns a new hash with the results of running the block once for
# every value.
# This method does not change the keys.
#
# If no block is given, an enumerator is returned instead.
#
def
transform_values
(
&
b
)
return
to_enum
:transform_values
unless
block_given?
hash
=
{}
self
.
each_key
do
|
k
|
hash
[
k
]
=
yield
(
self
[
k
])
end
hash
end
end
mrbgems/mruby-hash-ext/test/hash.rb
View file @
de3edcc1
...
...
@@ -254,3 +254,29 @@ assert("Hash#dig") do
assert_equal
(
1
,
h
.
dig
(
:a
,
:b
,
:c
))
assert_nil
(
h
.
dig
(
:d
))
end
assert
(
"Hash#transform_keys"
)
do
h
=
{
a
:{
b
:{
c
:
1
}}}
assert_equal
(
1
,
h
.
dig
(
:a
,
:b
,
:c
))
assert_nil
(
h
.
dig
(
:d
))
end
assert
(
"Hash#transform_keys"
)
do
h
=
{
"1"
=>
100
,
"2"
=>
200
}
assert_equal
(
h
.
transform_keys
{
|
k
|
k
+
"!"
},
{
"1!"
=>
100
,
"2!"
=>
200
})
assert_equal
(
h
.
transform_keys
{
|
k
|
k
.
to_i
},
{
1
=>
100
,
2
=>
200
})
assert_equal
(
h
.
transform_keys
.
with_index
{
|
k
,
i
|
"
#{
k
}
.
#{
i
}
"
},
{
"1.0"
=>
100
,
"2.1"
=>
200
})
end
assert
(
"Hash#transform_values"
)
do
h
=
{
a:
1
,
b:
2
,
c:
3
}
assert_equal
(
h
.
transform_values
{
|
v
|
v
*
v
+
1
},
{
a:
2
,
b:
5
,
c:
10
})
assert_equal
(
h
.
transform_values
{
|
v
|
v
.
to_s
},
{
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"
})
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