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
02b2ac8d
Commit
02b2ac8d
authored
Sep 19, 2014
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2594 from yasuyuki/hash
Implement Hash[]
parents
e4bd48f3
5f16c417
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
90 additions
and
0 deletions
+90
-0
mrbgems/mruby-hash-ext/mrblib/hash.rb
mrbgems/mruby-hash-ext/mrblib/hash.rb
+52
-0
mrbgems/mruby-hash-ext/test/hash.rb
mrbgems/mruby-hash-ext/test/hash.rb
+38
-0
No files found.
mrbgems/mruby-hash-ext/mrblib/hash.rb
View file @
02b2ac8d
...
...
@@ -3,6 +3,58 @@ class Hash
# ISO does not define Hash#each_pair, so each_pair is defined in gem.
alias
each_pair
each
##
# call-seq:
# Hash[ key, value, ... ] -> new_hash
# Hash[ [ [key, value], ... ] ] -> new_hash
# Hash[ object ] -> new_hash
#
# Creates a new hash populated with the given objects.
#
# Similar to the literal <code>{ _key_ => _value_, ... }</code>. In the first
# form, keys and values occur in pairs, so there must be an even number of
# arguments.
#
# The second and third form take a single argument which is either an array
# of key-value pairs or an object convertible to a hash.
#
# Hash["a", 100, "b", 200] #=> {"a"=>100, "b"=>200}
# Hash[ [ ["a", 100], ["b", 200] ] ] #=> {"a"=>100, "b"=>200}
# Hash["a" => 100, "b" => 200] #=> {"a"=>100, "b"=>200}
#
def
self
.
[]
(
*
object
)
o
=
object
[
0
]
if
o
.
respond_to?
(
:to_hash
)
h
=
Hash
.
new
object
[
0
].
to_hash
.
each
{
|
k
,
v
|
h
[
k
]
=
v
}
return
h
elsif
o
.
respond_to?
(
:to_a
)
h
=
Hash
.
new
o
.
to_a
.
each
do
|
i
|
raise
ArgumentError
,
"wrong element type
#{
i
.
class
}
(expected array)"
unless
i
.
respond_to?
(
:to_a
)
k
,
v
=
nil
case
i
.
size
when
2
k
=
i
[
0
]
v
=
i
[
1
]
when
1
k
=
i
[
0
]
else
raise
ArgumentError
,
"invalid number of elements (
#{
i
.
size
}
for 1..2)"
end
h
[
k
]
=
v
end
return
h
end
raise
ArgumentError
,
'odd number of arguments for Hash'
unless
object
.
length
%
2
==
0
h
=
Hash
.
new
0
.
step
(
object
.
length
-
2
,
2
)
do
|
i
|
h
[
object
[
i
]]
=
object
[
i
+
1
]
end
h
end
##
# call-seq:
# hsh.merge!(other_hash) -> hsh
...
...
mrbgems/mruby-hash-ext/test/hash.rb
View file @
02b2ac8d
##
# Hash(Ext) Test
assert
(
'Hash.[] Hash'
)
do
a
=
Hash
[
'a_key'
=>
'a_value'
]
assert_equal
({
'a_key'
=>
'a_value'
},
a
)
end
assert
(
'Hash.[] [ [ ["b_key", "b_value" ] ] ]'
)
do
a
=
Hash
[
[
[
'b_key'
,
'b_value'
]
]
]
assert_equal
({
'b_key'
=>
'b_value'
},
a
)
a
=
Hash
[
[
]
]
assert_equal
({},
a
)
assert_raise
(
ArgumentError
)
do
Hash
[
[
[
'b_key'
,
'b_value'
,
'b_over'
]
]
]
end
assert_raise
(
ArgumentError
)
do
Hash
[
[
[]
]
]
end
end
assert
(
'Hash.[] "c_key", "c_value"'
)
do
a
=
Hash
[
'c_key'
,
'c_value'
,
'd_key'
,
1
]
assert_equal
({
'c_key'
=>
'c_value'
,
'd_key'
=>
1
},
a
)
a
=
Hash
[]
assert_equal
({},
a
)
assert_raise
(
ArgumentError
)
do
Hash
[
'd_key'
]
end
end
assert
(
'Hash#merge!'
)
do
a
=
{
'abc_key'
=>
'abc_value'
,
'cba_key'
=>
'cba_value'
}
b
=
{
'cba_key'
=>
'XXX'
,
'xyz_key'
=>
'xyz_value'
}
...
...
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