aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support
diff options
context:
space:
mode:
authorRizwan Reza <rizwanreza@gmail.com>2010-06-05 04:04:49 +0430
committerRizwan Reza <rizwanreza@gmail.com>2010-06-05 04:04:49 +0430
commite6f2102178b19ac8d49363b5fa145f22483c30ee (patch)
treeb5dff24210eb3f521087fe66a9b734165ad22967 /activesupport/lib/active_support
parent1535b02a9f8e0eaa3cd3182a45d2bd7855c3809c (diff)
parent8e8cb1769f3a78c53e5b79d0ce6a08589045cfca (diff)
downloadrails-e6f2102178b19ac8d49363b5fa145f22483c30ee.tar.gz
rails-e6f2102178b19ac8d49363b5fa145f22483c30ee.tar.bz2
rails-e6f2102178b19ac8d49363b5fa145f22483c30ee.zip
Merge branch 'master' of git://github.com/rails/rails
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r--activesupport/lib/active_support/core_ext/array/random_access.rb22
-rw-r--r--activesupport/lib/active_support/core_ext/kernel/debugger.rb2
2 files changed, 19 insertions, 5 deletions
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 67c322daea..7a4836cecd 100644
--- a/activesupport/lib/active_support/core_ext/array/random_access.rb
+++ b/activesupport/lib/active_support/core_ext/array/random_access.rb
@@ -1,6 +1,20 @@
class Array
- # Returns a random element from the array.
- def random_element
- self[Kernel.rand(length)]
- end
+ # Backport of Array#sample based on Marc-Andre Lafortune's http://github.com/marcandre/backports/
+ def sample(n=nil)
+ return self[Kernel.rand(size)] if n.nil?
+ n = n.to_int
+ rescue Exception => e
+ raise TypeError, "Coercion error: #{n.inspect}.to_int => Integer failed:\n(#{e.message})"
+ else
+ raise TypeError, "Coercion error: obj.to_int did NOT return an Integer (was #{n.class})" unless n.kind_of? Integer
+ raise ArgumentError, "negative array size" if n < 0
+ n = size if n > size
+ result = Array.new(self)
+ n.times do |i|
+ r = i + Kernel.rand(size - i)
+ result[i], result[r] = result[r], result[i]
+ end
+ result[n..size] = []
+ result
+ end unless method_defined? :sample
end \ No newline at end of file
diff --git a/activesupport/lib/active_support/core_ext/kernel/debugger.rb b/activesupport/lib/active_support/core_ext/kernel/debugger.rb
index 22fcc1910b..692340c7c7 100644
--- a/activesupport/lib/active_support/core_ext/kernel/debugger.rb
+++ b/activesupport/lib/active_support/core_ext/kernel/debugger.rb
@@ -2,7 +2,7 @@ module Kernel
unless respond_to?(:debugger)
# Starts a debugging session if ruby-debug has been loaded (call rails server --debugger to do load it).
def debugger
- message = "\n***** Debugger requested, but was not available: Start server with --debugger to enable *****\n"
+ message = "\n***** Debugger requested, but was not available (ensure ruby-debug is listed in Gemfile/installed as gem): Start server with --debugger to enable *****\n"
defined?(Rails) ? Rails.logger.info(message) : $stderr.puts(message)
end
end