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
4be6f1c7
Commit
4be6f1c7
authored
May 04, 2014
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2181 from suzukaze/add-hash_delete_if
Add Hash#delete_if
parents
8e4d3bb9
3e7db4ad
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
57 additions
and
0 deletions
+57
-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
+34
-0
No files found.
mrbgems/mruby-hash-ext/mrblib/hash.rb
View file @
4be6f1c7
...
@@ -27,4 +27,27 @@ class Hash
...
@@ -27,4 +27,27 @@ class Hash
self
[
key
]
self
[
key
]
end
end
end
end
##
# call-seq:
# hsh.delete_if {| key, value | block } -> hsh
# hsh.delete_if -> an_enumerator
#
# Deletes every key-value pair from <i>hsh</i> for which <i>block</i>
# evaluates to <code>true</code>.
#
# If no block is given, an enumerator is returned instead.
#
# h = { "a" => 100, "b" => 200, "c" => 300 }
# h.delete_if {|key, value| key >= "b" } #=> {"a"=>100}
#
def
delete_if
(
&
block
)
return
to_enum
:delete_if
unless
block_given?
self
.
each
do
|
k
,
v
|
self
.
delete
(
k
)
if
block
.
call
(
k
,
v
)
end
self
end
end
end
mrbgems/mruby-hash-ext/test/hash.rb
View file @
4be6f1c7
...
@@ -39,3 +39,37 @@ assert('Hash#fetch') do
...
@@ -39,3 +39,37 @@ assert('Hash#fetch') do
assert_kind_of
(
StandardError
,
e
);
assert_kind_of
(
StandardError
,
e
);
end
end
end
end
assert
(
"Hash#delete_if"
)
do
base
=
{
1
=>
'one'
,
2
=>
false
,
true
=>
'true'
,
'cat'
=>
99
}
h1
=
{
1
=>
'one'
,
2
=>
false
,
true
=>
'true'
}
h2
=
{
2
=>
false
,
'cat'
=>
99
}
h3
=
{
2
=>
false
}
h
=
base
.
dup
assert_equal
(
h
,
h
.
delete_if
{
false
})
assert_equal
({},
h
.
delete_if
{
true
})
h
=
base
.
dup
assert_equal
(
h1
,
h
.
delete_if
{
|
k
,
v
|
k
.
instance_of?
(
String
)
})
assert_equal
(
h1
,
h
)
h
=
base
.
dup
assert_equal
(
h2
,
h
.
delete_if
{
|
k
,
v
|
v
.
instance_of?
(
String
)
})
assert_equal
(
h2
,
h
)
h
=
base
.
dup
assert_equal
(
h3
,
h
.
delete_if
{
|
k
,
v
|
v
})
assert_equal
(
h3
,
h
)
h
=
base
.
dup
n
=
0
h
.
delete_if
{
|*
a
|
n
+=
1
assert_equal
(
2
,
a
.
size
)
assert_equal
(
base
[
a
[
0
]],
a
[
1
])
h
.
shift
true
}
assert_equal
(
base
.
size
,
n
)
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