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
571f0d4a
Commit
571f0d4a
authored
May 04, 2014
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2186 from suzukaze/add-hash.flatten
Add Hash#flatten
parents
e9e4c133
28a858c1
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 @
571f0d4a
...
@@ -100,4 +100,24 @@ class Hash
...
@@ -100,4 +100,24 @@ class Hash
end
end
self
self
end
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
end
mrbgems/mruby-hash-ext/test/hash.rb
View file @
571f0d4a
...
@@ -73,3 +73,12 @@ assert("Hash#delete_if") do
...
@@ -73,3 +73,12 @@ assert("Hash#delete_if") do
}
}
assert_equal
(
base
.
size
,
n
)
assert_equal
(
base
.
size
,
n
)
end
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