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
08f2bb0b
Commit
08f2bb0b
authored
May 04, 2014
by
Jun Hiroe
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Hash comments
parent
0fe62ddc
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
50 additions
and
0 deletions
+50
-0
mrbgems/mruby-hash-ext/mrblib/hash.rb
mrbgems/mruby-hash-ext/mrblib/hash.rb
+50
-0
No files found.
mrbgems/mruby-hash-ext/mrblib/hash.rb
View file @
08f2bb0b
class
Hash
##
# call-seq:
# hsh.merge!(other_hash) -> hsh
# hsh.merge!(other_hash){|key, oldval, newval| block} -> hsh
#
# Adds the contents of _other_hash_ to _hsh_. If no block is specified,
# entries with duplicate keys are overwritten with the values from
# _other_hash_, otherwise the value of each duplicate key is determined by
# calling the block with the key, its value in _hsh_ and its value in
# _other_hash_.
#
# h1 = { "a" => 100, "b" => 200 }
# h2 = { "b" => 254, "c" => 300 }
# h1.merge!(h2) #=> {"a"=>100, "b"=>254, "c"=>300}
#
# h1 = { "a" => 100, "b" => 200 }
# h2 = { "b" => 254, "c" => 300 }
# h1.merge!(h2) { |key, v1, v2| v1 }
# #=> {"a"=>100, "b"=>200, "c"=>300}
#
def
merge!
(
other
,
&
block
)
raise
"can't convert argument into Hash"
unless
other
.
respond_to?
(
:to_hash
)
if
block
...
...
@@ -14,6 +36,34 @@ class Hash
alias
each_pair
each
alias
update
merge!
##
# call-seq:
# hsh.fetch(key [, default] ) -> obj
# hsh.fetch(key) {| key | block } -> obj
#
# Returns a value from the hash for the given key. If the key can't be
# found, there are several options: With no other arguments, it will
# raise an <code>KeyError</code> exception; if <i>default</i> is
# given, then that will be returned; if the optional code block is
# specified, then that will be run and its result returned.
#
# h = { "a" => 100, "b" => 200 }
# h.fetch("a") #=> 100
# h.fetch("z", "go fish") #=> "go fish"
# h.fetch("z") { |el| "go fish, #{el}"} #=> "go fish, z"
#
# The following example shows that an exception is raised if the key
# is not found and a default value is not supplied.
#
# h = { "a" => 100, "b" => 200 }
# h.fetch("z")
#
# <em>produces:</em>
#
# prog.rb:2:in `fetch': key not found (KeyError)
# from prog.rb:2
#
def
fetch
(
key
,
none
=
NONE
,
&
block
)
unless
self
.
key?
(
key
)
if
block
...
...
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