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
dcad562f
Commit
dcad562f
authored
May 05, 2014
by
Jun Hiroe
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Hash#keep_if
parent
1ce9060f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
0 deletions
+30
-0
mrbgems/mruby-hash-ext/mrblib/hash.rb
mrbgems/mruby-hash-ext/mrblib/hash.rb
+23
-0
mrbgems/mruby-hash-ext/test/hash.rb
mrbgems/mruby-hash-ext/test/hash.rb
+7
-0
No files found.
mrbgems/mruby-hash-ext/mrblib/hash.rb
View file @
dcad562f
...
@@ -137,4 +137,27 @@ class Hash
...
@@ -137,4 +137,27 @@ class Hash
self
.
each
{
|
k
,
v
|
h
[
v
]
=
k
}
self
.
each
{
|
k
,
v
|
h
[
v
]
=
k
}
h
h
end
end
##
# call-seq:
# hsh.keep_if {| key, value | block } -> hsh
# hsh.keep_if -> an_enumerator
#
# Deletes every key-value pair from <i>hsh</i> for which <i>block</i>
# evaluates to false.
#
# If no block is given, an enumerator is returned instead.
#
def
keep_if
(
&
block
)
return
to_enum
:keep_if
unless
block_given?
keys
=
[]
self
.
each
do
|
k
,
v
|
unless
block
.
call
([
k
,
v
])
self
.
delete
(
k
)
end
end
self
end
end
end
mrbgems/mruby-hash-ext/test/hash.rb
View file @
dcad562f
...
@@ -95,3 +95,10 @@ assert("Hash#invert") do
...
@@ -95,3 +95,10 @@ assert("Hash#invert") do
assert_include
(
%w[a c]
,
h
[
1
])
assert_include
(
%w[a c]
,
h
[
1
])
assert_equal
(
'b'
,
h
[
2
])
assert_equal
(
'b'
,
h
[
2
])
end
end
assert
(
"Hash#keep_if"
)
do
h
=
{
1
=>
2
,
3
=>
4
,
5
=>
6
}
assert_equal
({
3
=>
4
,
5
=>
6
},
h
.
keep_if
{
|
k
,
v
|
k
+
v
>=
7
})
h
=
{
1
=>
2
,
3
=>
4
,
5
=>
6
}
assert_equal
({
1
=>
2
,
3
=>
4
,
5
=>
6
}
,
h
.
keep_if
{
true
})
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