---
tags : Ruby
---
# Array
**unshift**
arr = [1,2,3,4,5,6]
add a new item to the beginning of an array.
arr.unshift(0) #=> [0, 1, 2, 3, 4, 5, 6]
**compact**
Remove nil values from an array
arr = ['foo', 0, nil, 'bar', 7, 'baz', nil]
arr.compact #=> ['foo', 0, 'bar', 7, 'baz']
arr #=> ['foo', 0, nil, 'bar', 7, 'baz', nil]
arr.compact! #=> ['foo', 0, 'bar', 7, 'baz']
arr #=> ['foo', 0, 'bar', 7, 'baz']
在 Array 上我们有 .compact 和 .compact! ,两种方法都会改变数组,
但是 .compact! 如果数组中没有 nil,则返回 nil 而不是 self.