Permalink
Browse files

Add Array#tail

  • Loading branch information...
fuadsaud committed Aug 22, 2013
1 parent 4112ada commit 52bbc166a514cd3917043c051ff39a749577fd88
Showing with 31 additions and 0 deletions.
  1. +2 −0 README.md
  2. +2 −0 lib/powerpack.rb
  3. +1 −0 lib/powerpack/array.rb
  4. +15 −0 lib/powerpack/array/tail.rb
  5. +11 −0 spec/powerpack/array/tail_spec.rb
View
@@ -21,6 +21,8 @@ Or install it yourself as:
## Extensions
+* [Array](http://rdoc.info/github/bbatsov/powerpack/Array)
+ * [#tail](http://rdoc.info/github/bbatsov/powerpack/Array#tail-instance_method)
* [Hash](http://rdoc.info/github/bbatsov/powerpack/Hash)
* [#symbolize_keys](http://rdoc.info/github/bbatsov/powerpack/Hash#symbolize_keys-instance_method)
* [Numeric](http://rdoc.info/github/bbatsov/powerpack/Numeric)
View
@@ -5,3 +5,5 @@
require 'powerpack/numeric'
require 'powerpack/string'
+
+require 'powerpack/array'
View
@@ -0,0 +1 @@
+require_relative 'array/tail'
@@ -0,0 +1,15 @@
+unless Array.method_defined? :tail
+ class Array
+ # Returns a new array rejecting the current one's first element.
+ #
+ # @return [Array] a new array without the first element or nil if this
+ # array is empty
+ #
+ # @example
+ # [1, 2, 3].tail #=> [2, 3]
+ # [].tail #=> nil
+ def tail
+ self[1..-1]
+ end
+ end
+end
@@ -0,0 +1,11 @@
+require 'spec_helper'
+
+describe 'Array#tail' do
+ it 'returns an array without the first element' do
+ expect([1, 2, 3].tail).to eq([2, 3])
+ end
+
+ it 'returns nil for empty lists' do
+ expect([].tail).to be_nil
+ end
+end

0 comments on commit 52bbc16

Please sign in to comment.