From 91761b775ce1f028486dc3483904795e9c028ed6 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Fri, 15 Apr 2011 14:13:10 -0300 Subject: Added an example of exception situation on Array#sample docs --- activesupport/lib/active_support/core_ext/array/random_access.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'activesupport/lib/active_support/core_ext/array') diff --git a/activesupport/lib/active_support/core_ext/array/random_access.rb b/activesupport/lib/active_support/core_ext/array/random_access.rb index 0bf510e39a..9eba4642b8 100644 --- a/activesupport/lib/active_support/core_ext/array/random_access.rb +++ b/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 []. # - # [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 -- cgit v1.2.3