From 0175c470c01c22db2f54c3912958bdb6c573d6cb Mon Sep 17 00:00:00 2001 From: Thiago Pradi Date: Mon, 28 Jun 2010 13:31:57 -0300 Subject: Documentation for Array#sample --- activesupport/lib/active_support/core_ext/array/random_access.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'activesupport/lib/active_support') 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 7a4836cecd..edac7278bc 100644 --- a/activesupport/lib/active_support/core_ext/array/random_access.rb +++ b/activesupport/lib/active_support/core_ext/array/random_access.rb @@ -1,5 +1,12 @@ class Array # Backport of Array#sample based on Marc-Andre Lafortune's http://github.com/marcandre/backports/ + # Returns a random element or +n+ random elements from the array. + # If the array is empty and +n+ is nil, returns nil. if +n+ is passed, returns []. + # + # [1,2,3,4,5,6].sample # => 4 + # [1,2,3,4,5,6].sample(3) # => [2, 4, 5] + # [].sample # => nil + # [].sample(3) # => [] def sample(n=nil) return self[Kernel.rand(size)] if n.nil? n = n.to_int -- cgit v1.2.3