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
ad638fc1
Commit
ad638fc1
authored
May 14, 2013
by
skandhas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add comments for mruby-array-ext
parent
077213c5
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
106 additions
and
0 deletions
+106
-0
mrbgems/mruby-array-ext/mrblib/array.rb
mrbgems/mruby-array-ext/mrblib/array.rb
+106
-0
No files found.
mrbgems/mruby-array-ext/mrblib/array.rb
View file @
ad638fc1
class
Array
class
Array
##
# call-seq:
# ary.uniq! -> ary or nil
#
# Removes duplicate elements from +self+.
# Returns <code>nil</code> if no changes are made (that is, no
# duplicates are found).
#
# a = [ "a", "a", "b", "b", "c" ]
# a.uniq! #=> ["a", "b", "c"]
# b = [ "a", "b", "c" ]
# b.uniq! #=> nil
#
def
uniq!
def
uniq!
ary
=
self
.
dup
ary
=
self
.
dup
result
=
[]
result
=
[]
...
@@ -13,12 +26,32 @@ class Array
...
@@ -13,12 +26,32 @@ class Array
end
end
end
end
##
# call-seq:
# ary.uniq -> new_ary
#
# Returns a new array by removing duplicate values in +self+.
#
# a = [ "a", "a", "b", "b", "c" ]
# a.uniq #=> ["a", "b", "c"]
#
def
uniq
def
uniq
ary
=
self
.
dup
ary
=
self
.
dup
ary
.
uniq!
ary
.
uniq!
ary
ary
end
end
##
# call-seq:
# ary - other_ary -> new_ary
#
# Array Difference---Returns a new array that is a copy of
# the original array, removing any items that also appear in
# <i>other_ary</i>. (If you need set-like behavior, see the
# library class Set.)
#
# [ 1, 1, 2, 2, 3, 3, 4, 5 ] - [ 1, 2, 4 ] #=> [ 3, 3, 5 ]
#
def
-
(
elem
)
def
-
(
elem
)
raise
TypeError
,
"can't convert
#{
elem
.
class
}
into Array"
unless
elem
.
class
==
Array
raise
TypeError
,
"can't convert
#{
elem
.
class
}
into Array"
unless
elem
.
class
==
Array
...
@@ -29,6 +62,16 @@ class Array
...
@@ -29,6 +62,16 @@ class Array
array
array
end
end
##
# call-seq:
# ary | other_ary -> new_ary
#
# Set Union---Returns a new array by joining this array with
# <i>other_ary</i>, removing duplicates.
#
# [ "a", "b", "c" ] | [ "c", "d", "a" ]
# #=> [ "a", "b", "c", "d" ]
#
def
|
(
elem
)
def
|
(
elem
)
raise
TypeError
,
"can't convert
#{
elem
.
class
}
into Array"
unless
elem
.
class
==
Array
raise
TypeError
,
"can't convert
#{
elem
.
class
}
into Array"
unless
elem
.
class
==
Array
...
@@ -36,6 +79,15 @@ class Array
...
@@ -36,6 +79,15 @@ class Array
ary
.
uniq!
or
ary
ary
.
uniq!
or
ary
end
end
##
# call-seq:
# ary & other_ary -> new_ary
#
# Set Intersection---Returns a new array
# containing elements common to the two arrays, with no duplicates.
#
# [ 1, 1, 3, 5 ] & [ 1, 2, 3 ] #=> [ 1, 3 ]
#
def
&
(
elem
)
def
&
(
elem
)
raise
TypeError
,
"can't convert
#{
elem
.
class
}
into Array"
unless
elem
.
class
==
Array
raise
TypeError
,
"can't convert
#{
elem
.
class
}
into Array"
unless
elem
.
class
==
Array
...
@@ -51,6 +103,23 @@ class Array
...
@@ -51,6 +103,23 @@ class Array
array
array
end
end
##
# call-seq:
# ary.flatten -> new_ary
# ary.flatten(level) -> new_ary
#
# Returns a new array that is a one-dimensional flattening of this
# array (recursively). That is, for every element that is an array,
# extract its elements into the new array. If the optional
# <i>level</i> argument determines the level of recursion to flatten.
#
# s = [ 1, 2, 3 ] #=> [1, 2, 3]
# t = [ 4, 5, 6, [7, 8] ] #=> [4, 5, 6, [7, 8]]
# a = [ s, t, 9, 10 ] #=> [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]
# a.flatten #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# a = [ 1, 2, [3, [4, 5] ] ]
# a.flatten(1) #=> [1, 2, 3, [4, 5]]
#
def
flatten
(
depth
=
nil
)
def
flatten
(
depth
=
nil
)
ar
=
[]
ar
=
[]
self
.
each
do
|
e
|
self
.
each
do
|
e
|
...
@@ -63,6 +132,23 @@ class Array
...
@@ -63,6 +132,23 @@ class Array
ar
ar
end
end
##
# call-seq:
# ary.flatten! -> ary or nil
# ary.flatten!(level) -> array or nil
#
# Flattens +self+ in place.
# Returns <code>nil</code> if no modifications were made (i.e.,
# <i>ary</i> contains no subarrays.) If the optional <i>level</i>
# argument determines the level of recursion to flatten.
#
# a = [ 1, 2, [3, [4, 5] ] ]
# a.flatten! #=> [1, 2, 3, 4, 5]
# a.flatten! #=> nil
# a #=> [1, 2, 3, 4, 5]
# a = [ 1, 2, [3, [4, 5] ] ]
# a.flatten!(1) #=> [1, 2, 3, [4, 5]]
#
def
flatten!
(
depth
=
nil
)
def
flatten!
(
depth
=
nil
)
modified
=
false
modified
=
false
ar
=
[]
ar
=
[]
...
@@ -81,12 +167,32 @@ class Array
...
@@ -81,12 +167,32 @@ class Array
end
end
end
end
##
# call-seq:
# ary.compact -> new_ary
#
# Returns a copy of +self+ with all +nil+ elements removed.
#
# [ "a", nil, "b", nil, "c", nil ].compact
# #=> [ "a", "b", "c" ]
#
def
compact
def
compact
result
=
self
.
dup
result
=
self
.
dup
result
.
compact!
result
.
compact!
result
result
end
end
##
# call-seq:
# ary.compact! -> ary or nil
#
# Removes +nil+ elements from the array.
# Returns +nil+ if no changes were made, otherwise returns
# <i>ary</i>.
#
# [ "a", nil, "b", nil, "c" ].compact! #=> [ "a", "b", "c" ]
# [ "a", "b", "c" ].compact! #=> nil
#
def
compact!
def
compact!
result
=
self
.
select
{
|
e
|
e
!=
nil
}
result
=
self
.
select
{
|
e
|
e
!=
nil
}
if
result
.
size
==
self
.
size
if
result
.
size
==
self
.
size
...
...
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