Skip to content
Browse files

add Enumerable#drop_last:

  • Loading branch information...
1 parent f603802 commit 9ff3195755486d5c76b56246546a3ffa3a1501b3 @bakkdoor committed Oct 31, 2012
Showing with 31 additions and 0 deletions.
  1. +12 −0 lib/enumerable.fy
  2. +19 −0 tests/string.fy
View
12 lib/enumerable.fy
@@ -504,6 +504,18 @@ class Fancy {
alias_method: 'skip: for: 'drop:
+ def drop_last: amount (1) {
+ """
+ @amount Amount of elements to drop from the end.
+ @return New @Array@ without last @amount elements.
+
+ Example:
+ [1,2,3,4] drop_last: 2 # => [3,4]
+ """
+
+ first: (size - amount)
+ }
+
def reduce: block init_val: init_val {
"""
Calculates a value based on a given block to be called on an accumulator
View
19 tests/string.fy
@@ -55,6 +55,25 @@ FancySpec describe: String with: {
"hello world" drop_while: |c| { c != " " } . join: "" . is: " world"
}
+ it: "drops the last element" with: 'drop_last: when: {
+ [] drop_last is: []
+ [1] drop_last is: []
+ "foo" drop_last is: ["f", "o"]
+ (1,2,3) drop_last is: [1,2]
+ }
+
+ it: "rops the last n elements" with: 'drop_last: when: {
+ [] drop_last: 1 . is: []
+ [] drop_last: 2 . is: []
+ [] drop_last: 0 . is: []
+ [1] drop_last: 0 . is: [1]
+ [1] drop_last: 1 . is: []
+ [1] drop_last: 2 . is: []
+ [1,2,3] drop_last: 1 . is: [1, 2]
+ [1,2,3] drop_last: 2 . is: [1]
+ [1,2,3] drop_last: 3 . is: []
+ }
+
it: "is empty" with: 'empty? when: {
"" empty? is: true
" " empty? is: false

0 comments on commit 9ff3195

Please sign in to comment.
Something went wrong with that request. Please try again.