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
14a92f97
Commit
14a92f97
authored
Dec 27, 2020
by
Seeker
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add `Hash#except` [Ruby 3.0]
parent
d57d6e2b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
0 deletions
+33
-0
mrbgems/mruby-hash-ext/src/hash-ext.c
mrbgems/mruby-hash-ext/src/hash-ext.c
+26
-0
mrbgems/mruby-hash-ext/test/hash.rb
mrbgems/mruby-hash-ext/test/hash.rb
+7
-0
No files found.
mrbgems/mruby-hash-ext/src/hash-ext.c
View file @
14a92f97
...
...
@@ -69,6 +69,31 @@ hash_slice(mrb_state *mrb, mrb_value hash)
return
result
;
}
/*
* call-seq:
* hsh.except(*keys) -> a_hash
*
* Returns a hash excluding the given keys and their values.
*
* h = { a: 100, b: 200, c: 300 }
* h.except(:a) #=> {:b=>200, :c=>300}
* h.except(:b, :c, :d) #=> {:a=>100}
*/
static
mrb_value
hash_except
(
mrb_state
*
mrb
,
mrb_value
hash
)
{
const
mrb_value
*
argv
;
mrb_value
result
;
mrb_int
argc
,
i
;
mrb_get_args
(
mrb
,
"*"
,
&
argv
,
&
argc
);
result
=
mrb_hash_dup
(
mrb
,
hash
);
for
(
i
=
0
;
i
<
argc
;
i
++
)
{
mrb_hash_delete_key
(
mrb
,
result
,
argv
[
i
]);
}
return
result
;
}
void
mrb_mruby_hash_ext_gem_init
(
mrb_state
*
mrb
)
{
...
...
@@ -77,6 +102,7 @@ mrb_mruby_hash_ext_gem_init(mrb_state *mrb)
h
=
mrb
->
hash_class
;
mrb_define_method
(
mrb
,
h
,
"values_at"
,
hash_values_at
,
MRB_ARGS_ANY
());
mrb_define_method
(
mrb
,
h
,
"slice"
,
hash_slice
,
MRB_ARGS_ANY
());
mrb_define_method
(
mrb
,
h
,
"except"
,
hash_except
,
MRB_ARGS_ANY
());
}
void
...
...
mrbgems/mruby-hash-ext/test/hash.rb
View file @
14a92f97
...
...
@@ -288,3 +288,10 @@ assert("Hash#slice") do
assert_equal
({
:a
=>
100
},
h
.
slice
(
:a
))
assert_equal
({
:b
=>
200
,
:c
=>
300
},
h
.
slice
(
:b
,
:c
,
:d
))
end
assert
(
"Hash#except"
)
do
h
=
{
a:
100
,
b:
200
,
c:
300
}
assert_equal
({
:b
=>
200
,
:c
=>
300
},
h
.
except
(
:a
))
assert_equal
({
:a
=>
100
},
h
.
except
(
:b
,
:c
,
:d
))
assert_equal
(
h
,
h
.
except
)
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