zip(arg, ...)
x = [ 4, 5, 6 ]
y = [ 7, 8, 9 ]
[1, 2, 3].zip(x, y) #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
[1, 2].zip(x, y) #=> [[1, 4, 7], [2, 5, 8]]
x.zip([1, 2], [8]) #=> [[4, 1, 8], [5, 2, nil], [6, nil, nil]]
b = [[1,2,3],['a','b','c'],['x','y','z']]
[[4,4,4],[5,5,5],[6,6,6]].zip(*b)
#=> [[[4, 4, 4], 1, "a", "x"], [[5, 5, 5], 2, "b", "y"], [[6, 6, 6], 3, "c", "z"]]
uniq
uniq{ |item| ... }
Returns a new array by removing duplicate values inself
.
a = [ "a", "a", "b", "b", "c" ]
a.uniq # => ["a", "b", "c"]
b = [["student","sam"], ["student","george"], ["teacher","matz"]]
b.uniq { |s| s.first } #=> [["student", "sam"], ["teacher", "matz"]]
b = [["student","sam"], ["student","george"], ["teacher","matz"], ["george","sam"]]
b.uniq { |s| s.first } #=> [["student", "sam"], ["teacher", "matz"], ["george", "sam"]]
b = [["student","sam"], ["student","george"], ["teacher","matz"], ["george","sam"], ["teacher","sam"]]
b.uniq { |s| s.first } #=> [["student", "sam"], ["teacher", "matz"], ["george", "sam"]]
flatten
The optional level
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]]