Skip to content
Browse files

Added an example of exception situation on Array#sample docs

  • Loading branch information...
1 parent 4dd84c8 commit 91761b775ce1f028486dc3483904795e9c028ed6 @smartinez87 smartinez87 committed
Showing with 5 additions and 4 deletions.
  1. +5 −4 activesupport/lib/active_support/core_ext/array/random_access.rb
View
9 activesupport/lib/active_support/core_ext/array/random_access.rb
@@ -5,10 +5,11 @@ class Array
# If +n+ is passed and its value is less than 0, it raises an +ArgumentError+ exception.
# If the value of +n+ is equal or greater than 0 it returns <tt>[]</tt>.
#
- # [1,2,3,4,5,6].sample # => 4
- # [1,2,3,4,5,6].sample(3) # => [2, 4, 5]
- # [].sample # => nil
- # [].sample(3) # => []
+ # [1,2,3,4,5,6].sample # => 4
+ # [1,2,3,4,5,6].sample(3) # => [2, 4, 5]
+ # [1,2,3,4,5,6].sample(-3) # => ArgumentError: negative sample number
+ # [].sample # => nil
+ # [].sample(3) # => []
def sample(n=nil)
return self[Kernel.rand(size)] if n.nil?
n = n.to_int

0 comments on commit 91761b7

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