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
41f5c5b2
Commit
41f5c5b2
authored
Mar 23, 2017
by
Yukihiro "Matz" Matsumoto
Committed by
GitHub
Mar 23, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3529 from kou/hash-ext-sub-class
Hash sub class creates new sub class objects instead of Hash
parents
8ca98231
e72e50b5
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
16 additions
and
4 deletions
+16
-4
mrbgems/mruby-hash-ext/mrblib/hash.rb
mrbgems/mruby-hash-ext/mrblib/hash.rb
+4
-4
mrbgems/mruby-hash-ext/test/hash.rb
mrbgems/mruby-hash-ext/test/hash.rb
+12
-0
No files found.
mrbgems/mruby-hash-ext/mrblib/hash.rb
View file @
41f5c5b2
...
...
@@ -28,11 +28,11 @@ class Hash
if
length
==
1
o
=
object
[
0
]
if
o
.
respond_to?
(
:to_hash
)
h
=
Hash
.
new
h
=
self
.
new
object
[
0
].
to_hash
.
each
{
|
k
,
v
|
h
[
k
]
=
v
}
return
h
elsif
o
.
respond_to?
(
:to_a
)
h
=
Hash
.
new
h
=
self
.
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
...
...
@@ -53,7 +53,7 @@ class Hash
unless
length
%
2
==
0
raise
ArgumentError
,
'odd number of arguments for Hash'
end
h
=
Hash
.
new
h
=
self
.
new
0
.
step
(
length
-
2
,
2
)
do
|
i
|
h
[
object
[
i
]]
=
object
[
i
+
1
]
end
...
...
@@ -211,7 +211,7 @@ class Hash
#
def
invert
h
=
Hash
.
new
h
=
self
.
class
.
new
self
.
each
{
|
k
,
v
|
h
[
v
]
=
k
}
h
end
...
...
mrbgems/mruby-hash-ext/test/hash.rb
View file @
41f5c5b2
...
...
@@ -39,6 +39,12 @@ assert('Hash.[] "c_key", "c_value"') do
end
end
assert
(
'Hash.[] for sub class'
)
do
sub_hash_class
=
Class
.
new
(
Hash
)
sub_hash
=
sub_hash_class
[]
assert_equal
(
sub_hash_class
,
sub_hash
.
class
)
end
assert
(
'Hash.try_convert'
)
do
assert_nil
Hash
.
try_convert
(
nil
)
assert_nil
Hash
.
try_convert
(
"{1=>2}"
)
...
...
@@ -143,6 +149,12 @@ assert("Hash#invert") do
assert_equal
(
'b'
,
h
[
2
])
end
assert
(
"Hash#invert with sub class"
)
do
sub_hash_class
=
Class
.
new
(
Hash
)
sub_hash
=
sub_hash_class
.
new
assert_equal
(
sub_hash_class
,
sub_hash
.
invert
.
class
)
end
assert
(
"Hash#keep_if"
)
do
h
=
{
1
=>
2
,
3
=>
4
,
5
=>
6
}
assert_equal
({
3
=>
4
,
5
=>
6
},
h
.
keep_if
{
|
k
,
v
|
k
+
v
>=
7
})
...
...
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