From c52ce1dae2ca8b72732f92c84541a4b6bbc9da77 Mon Sep 17 00:00:00 2001 From: Vasiliy Ermolovich Date: Wed, 21 Dec 2011 17:31:40 +0300 Subject: [PATCH] remove Array#sample from core_ext --- .../lib/active_support/core_ext/array.rb | 1 - .../core_ext/array/random_access.rb | 30 ------------------- activesupport/test/core_ext/array_ext_test.rb | 28 ----------------- 3 files changed, 59 deletions(-) delete mode 100644 activesupport/lib/active_support/core_ext/array/random_access.rb diff --git a/activesupport/lib/active_support/core_ext/array.rb b/activesupport/lib/active_support/core_ext/array.rb index 268c9bed4ca..79ba79192a5 100644 --- a/activesupport/lib/active_support/core_ext/array.rb +++ b/activesupport/lib/active_support/core_ext/array.rb @@ -4,5 +4,4 @@ require 'active_support/core_ext/array/uniq_by' require 'active_support/core_ext/array/conversions' require 'active_support/core_ext/array/extract_options' require 'active_support/core_ext/array/grouping' -require 'active_support/core_ext/array/random_access' require 'active_support/core_ext/array/prepend_and_append' diff --git a/activesupport/lib/active_support/core_ext/array/random_access.rb b/activesupport/lib/active_support/core_ext/array/random_access.rb deleted file mode 100644 index bb1807a68a2..00000000000 --- a/activesupport/lib/active_support/core_ext/array/random_access.rb +++ /dev/null @@ -1,30 +0,0 @@ -class Array - # Backport of Array#sample based on Marc-Andre Lafortune's https://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 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] - # [1,2,3,4,5,6].sample(-3) # => ArgumentError: negative array size - # [].sample # => nil - # [].sample(3) # => [] - 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 diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb index 52231aaeb09..6739b4afbda 100644 --- a/activesupport/test/core_ext/array_ext_test.rb +++ b/activesupport/test/core_ext/array_ext_test.rb @@ -363,34 +363,6 @@ class ArrayUniqByTests < Test::Unit::TestCase end end -class ArrayExtRandomTests < ActiveSupport::TestCase - def test_sample_from_array - assert_nil [].sample - assert_equal [], [].sample(5) - assert_equal 42, [42].sample - assert_equal [42], [42].sample(5) - - a = [:foo, :bar, 42] - s = a.sample(2) - assert_equal 2, s.size - assert_equal 1, (a-s).size - assert_equal [], a-(0..20).sum{a.sample(2)} - - o = Object.new - def o.to_int; 1; end - assert_equal [0], [0].sample(o) - - o = Object.new - assert_raises(TypeError) { [0].sample(o) } - - o = Object.new - def o.to_int; ''; end - assert_raises(TypeError) { [0].sample(o) } - - assert_raises(ArgumentError) { [0].sample(-7) } - end -end - class ArrayWrapperTests < Test::Unit::TestCase class FakeCollection def to_ary