Skip to content

Commit

Permalink
Update demo, test, and code file with example showing how order matters.
Browse files Browse the repository at this point in the history
  • Loading branch information
anithri committed Apr 19, 2015
1 parent 14212c7 commit 62b8ca3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
11 changes: 9 additions & 2 deletions demo/core/array/uniq_by.md
Expand Up @@ -2,8 +2,15 @@

require 'facets/array/uniq_by'

e = [-5, -4, -3, -2, -1, 0]
Like #uniq, but determines uniqueness based on a given block.
As can be seen from the examples, order is significant.

r = (-5..5).to_a
r.uniq_by!{ |i| i*i }
r.assert == e
r.assert == [-5, -4, -3, -2, -1, 0]

r = (-5..5).to_a.reverse
r.uniq_by!{ |i| i*i }
r.assert == [5, 4, 3, 2, 1, 0]


4 changes: 4 additions & 0 deletions lib/core/facets/array/uniq_by.rb
Expand Up @@ -9,6 +9,10 @@ class Array
# a.uniq_by!{ |i| i*i }
# a #=> [-5, -4, -3, -2, -1, 0]
#
# a = (-5..5).to_a.reverse
# a.uniq_by!{ |i| i*i }
# a #=> [5, 4, 3, 2, 1, 0]
#
# Returns [Array] of unique elements.
#
def uniq_by! #:yield:
Expand Down
7 changes: 7 additions & 0 deletions test/core/array/test_uniq_by.rb
Expand Up @@ -11,6 +11,13 @@
r.assert == e
end

test do
e = [5, 4, 3, 2, 1, 0]
r = (-5..5).to_a.reverse
r.uniq_by!{ |i| i*i }
r.assert == e
end

end

end
Expand Down

0 comments on commit 62b8ca3

Please sign in to comment.