Skip to content
This repository
Browse code

deprecates Array#random_element in favor of Array#sample, backported …

…from Ruby 1.9, thanks to Marc-Andre Lafortune
  • Loading branch information...
commit ed8cabcec24113e5effe327a313ea9c1fed600e0 1 parent 3d6ed50
Xavier Noria authored June 05, 2010
14  activerecord/test/cases/associations/eager_load_nested_include_test.rb
@@ -18,7 +18,7 @@ def remember; self.class.remembered << self; end
18 18
 
19 19
   module ClassMethods
20 20
     def remembered; @@remembered ||= []; end
21  
-    def random_element; @@remembered.random_element; end
  21
+    def sample; @@remembered.sample; end
22 22
   end
23 23
 end
24 24
 
@@ -82,14 +82,14 @@ def generate_test_object_graphs
82 82
       [Circle, Square, Triangle, NonPolyOne, NonPolyTwo].map(&:create!)
83 83
     end
84 84
     1.upto(NUM_SIMPLE_OBJS) do
85  
-      PaintColor.create!(:non_poly_one_id => NonPolyOne.random_element.id)
86  
-      PaintTexture.create!(:non_poly_two_id => NonPolyTwo.random_element.id)
  85
+      PaintColor.create!(:non_poly_one_id => NonPolyOne.sample.id)
  86
+      PaintTexture.create!(:non_poly_two_id => NonPolyTwo.sample.id)
87 87
     end
88 88
     1.upto(NUM_SHAPE_EXPRESSIONS) do
89  
-      shape_type = [Circle, Square, Triangle].random_element
90  
-      paint_type = [PaintColor, PaintTexture].random_element
91  
-      ShapeExpression.create!(:shape_type => shape_type.to_s, :shape_id => shape_type.random_element.id,
92  
-                              :paint_type => paint_type.to_s, :paint_id => paint_type.random_element.id)
  89
+      shape_type = [Circle, Square, Triangle].sample
  90
+      paint_type = [PaintColor, PaintTexture].sample
  91
+      ShapeExpression.create!(:shape_type => shape_type.to_s, :shape_id => shape_type.sample.id,
  92
+                              :paint_type => paint_type.to_s, :paint_id => paint_type.sample.id)
93 93
     end
94 94
   end
95 95
 
2  activerecord/test/cases/named_scope_test.rb
@@ -265,7 +265,7 @@ def test_find_all_should_behave_like_select
265 265
   end
266 266
 
267 267
   def test_rand_should_select_a_random_object_from_proxy
268  
-    assert Topic.approved.random_element.is_a?(Topic)
  268
+    assert Topic.approved.sample.is_a?(Topic)
269 269
   end
270 270
 
271 271
   def test_should_use_where_in_query_for_named_scope
3  activesupport/CHANGELOG
... ...
@@ -1,6 +1,7 @@
1 1
 *2.3.9 (unreleased)*
2 2
 
3  
-* ...
  3
+* Deprecates Array#random_element in favor of Array#sample, backported from Ruby 1.9, thanks to 
  4
+Marc-Andre Lafortune. [fxn]
4 5
 
5 6
 
6 7
 *2.3.8 (May 24, 2010)*
28  activesupport/lib/active_support/core_ext/array/random_access.rb
@@ -8,14 +8,34 @@ module RandomAccess
8 8
         #   https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/4555
9 9
         #
10 10
         def rand # :nodoc:
11  
-          ActiveSupport::Deprecation.warn 'Array#rand is deprecated and will be removed in Rails 3. Use "random_element" instead', caller
12  
-          random_element
  11
+          ActiveSupport::Deprecation.warn 'Array#rand is deprecated and will be removed in Rails 3. Use Array#sample instead', caller
  12
+          sample
13 13
         end
14 14
 
15 15
         # Returns a random element from the array.
16  
-        def random_element
17  
-          self[Kernel.rand(length)]
  16
+        def random_element # :nodoc:
  17
+          ActiveSupport::Deprecation.warn 'Array#random_element is deprecated and will be removed in Rails 3. Use Array#sample instead', caller
  18
+          sample
18 19
         end
  20
+
  21
+        # Backport of Array#sample based on Marc-Andre Lafortune's http://github.com/marcandre/backports/
  22
+        def sample(n=nil)
  23
+          return self[Kernel.rand(size)] if n.nil?
  24
+          n = n.to_int
  25
+        rescue Exception => e
  26
+          raise TypeError, "Coercion error: #{n.inspect}.to_int => Integer failed:\n(#{e.message})"
  27
+        else
  28
+          raise TypeError, "Coercion error: #{n}.to_int did NOT return an Integer (was #{n.class})" unless n.kind_of? ::Integer
  29
+          raise ArgumentError, "negative array size" if n < 0
  30
+          n = size if n > size
  31
+          result = ::Array.new(self)
  32
+          n.times do |i|
  33
+            r = i + Kernel.rand(size - i)
  34
+            result[i], result[r] = result[r], result[i]
  35
+          end
  36
+          result[n..size] = []
  37
+          result
  38
+        end unless method_defined? :sample
19 39
       end
20 40
     end
21 41
   end
34  activesupport/test/core_ext/array_ext_test.rb
@@ -324,19 +324,39 @@ def test_extract_options
324 324
 end
325 325
 
326 326
 class ArrayExtRandomTests < ActiveSupport::TestCase
327  
-  def test_random_element_from_array
328  
-    assert_nil [].random_element
329  
-
330  
-    Kernel.expects(:rand).with(1).returns(0)
331  
-    assert_equal 'x', ['x'].random_element
  327
+  def test_sample_from_array
  328
+    assert_nil [].sample
  329
+    assert_equal [], [].sample(5)
  330
+    assert_equal 42, [42].sample
  331
+    assert_equal [42], [42].sample(5)
  332
+
  333
+    a = [:foo, :bar, 42]
  334
+    s = a.sample(2)
  335
+    assert_equal 2, s.size
  336
+    assert_equal 1, (a-s).size
  337
+    assert_equal [], a-(0..20).sum{a.sample(2)}
  338
+  
  339
+    o = Object.new
  340
+    def o.to_int; 1; end
  341
+    assert_equal [0], [0].sample(o)
  342
+  
  343
+    o = Object.new
  344
+    assert_raises(TypeError) { [0].sample(o) }
  345
+    
  346
+    o = Object.new
  347
+    def o.to_int; ''; end
  348
+    assert_raises(TypeError) { [0].sample(o) }
332 349
 
333  
-    Kernel.expects(:rand).with(3).returns(1)
334  
-    assert_equal 2, [1, 2, 3].random_element
  350
+    assert_raises(ArgumentError) { [0].sample(-7) }
335 351
   end
336 352
 
337 353
   def test_deprecated_rand_on_array
338 354
     assert_deprecated { [].rand }
339 355
   end
  356
+
  357
+  def test_deprecated_random_element_on_array
  358
+    assert_deprecated { [].random_element }
  359
+  end
340 360
 end
341 361
 
342 362
 class ArrayWrapperTests < Test::Unit::TestCase

0 notes on commit ed8cabc

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