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
28a858c1
Commit
28a858c1
authored
May 04, 2014
by
Jun Hiroe
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Hash#flatten
parent
e9e4c133
Changes
2
Hide 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 @
28a858c1
...
...
@@ -100,4 +100,24 @@ class Hash
end
self
end
##
# call-seq:
# hash.flatten -> an_array
# hash.flatten(level) -> an_array
#
# Returns a new array that is a one-dimensional flattening of this
# hash. That is, for every key or value that is an array, extract
# its elements into the new array. Unlike Array#flatten, this
# method does not flatten recursively by default. The optional
# <i>level</i> argument determines the level of recursion to flatten.
#
# a = {1=> "one", 2 => [2,"two"], 3 => "three"}
# a.flatten # => [1, "one", 2, [2, "two"], 3, "three"]
# a.flatten(2) # => [1, "one", 2, 2, "two", 3, "three"]
#
def
flatten
(
level
=
1
)
self
.
to_a
.
flatten
(
level
)
end
end
mrbgems/mruby-hash-ext/test/hash.rb
View file @
28a858c1
...
...
@@ -73,3 +73,12 @@ assert("Hash#delete_if") do
}
assert_equal
(
base
.
size
,
n
)
end
assert
(
"Hash#flatten"
)
do
a
=
{
1
=>
"one"
,
2
=>
[
2
,
"two"
],
3
=>
[
3
,
[
"three"
]]}
assert_equal
[
1
,
"one"
,
2
,
[
2
,
"two"
],
3
,
[
3
,
[
"three"
]]],
a
.
flatten
assert_equal
[[
1
,
"one"
],
[
2
,
[
2
,
"two"
]],
[
3
,
[
3
,
[
"three"
]]]],
a
.
flatten
(
0
)
assert_equal
[
1
,
"one"
,
2
,
[
2
,
"two"
],
3
,
[
3
,
[
"three"
]]],
a
.
flatten
(
1
)
assert_equal
[
1
,
"one"
,
2
,
2
,
"two"
,
3
,
3
,
[
"three"
]],
a
.
flatten
(
2
)
assert_equal
[
1
,
"one"
,
2
,
2
,
"two"
,
3
,
3
,
"three"
],
a
.
flatten
(
3
)
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