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
dc4c042d
Commit
dc4c042d
authored
May 08, 2014
by
Jun Hiroe
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Hash#key
parent
c071ad13
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
0 deletions
+29
-0
mrbgems/mruby-hash-ext/mrblib/hash.rb
mrbgems/mruby-hash-ext/mrblib/hash.rb
+20
-0
mrbgems/mruby-hash-ext/test/hash.rb
mrbgems/mruby-hash-ext/test/hash.rb
+9
-0
No files found.
mrbgems/mruby-hash-ext/mrblib/hash.rb
View file @
dc4c042d
...
@@ -160,4 +160,24 @@ class Hash
...
@@ -160,4 +160,24 @@ class Hash
end
end
self
self
end
end
##
# call-seq:
# hsh.key(value) -> key
#
# Returns the key of an occurrence of a given value. If the value is
# not found, returns <code>nil</code>.
#
# h = { "a" => 100, "b" => 200, "c" => 300, "d" => 300 }
# h.key(200) #=> "b"
# h.key(300) #=> "c"
# h.key(999) #=> nil
#
def
key
(
val
)
self
.
each
do
|
k
,
v
|
return
k
if
v
==
val
end
nil
end
end
end
mrbgems/mruby-hash-ext/test/hash.rb
View file @
dc4c042d
...
@@ -102,3 +102,12 @@ assert("Hash#keep_if") do
...
@@ -102,3 +102,12 @@ assert("Hash#keep_if") do
h
=
{
1
=>
2
,
3
=>
4
,
5
=>
6
}
h
=
{
1
=>
2
,
3
=>
4
,
5
=>
6
}
assert_equal
({
1
=>
2
,
3
=>
4
,
5
=>
6
}
,
h
.
keep_if
{
true
})
assert_equal
({
1
=>
2
,
3
=>
4
,
5
=>
6
}
,
h
.
keep_if
{
true
})
end
end
assert
(
"Hash#key"
)
do
h
=
{
"a"
=>
100
,
"b"
=>
200
,
"c"
=>
300
,
"d"
=>
300
,
nil
=>
'nil'
,
'nil'
=>
nil
}
assert_equal
"b"
,
h
.
key
(
200
)
assert_equal
"c"
,
h
.
key
(
300
)
assert_nil
h
.
key
(
999
)
assert_nil
h
.
key
(
'nil'
)
assert_equal
'nil'
,
h
.
key
(
nil
)
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