Skip to content

Commit 821e15e

Browse files
spastorinofxn
authored andcommitted
Change on Array extension from rand => random_element [rails#4555 state:committed]
Signed-off-by: Xavier Noria <[email protected]>
1 parent 64d109e commit 821e15e

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

activerecord/test/cases/associations/eager_load_nested_include_test.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def remember; self.class.remembered << self; end
1717

1818
module ClassMethods
1919
def remembered; @@remembered ||= []; end
20-
def rand; @@remembered.rand; end
20+
def random_element; @@remembered.random_element; end
2121
end
2222
end
2323

@@ -79,14 +79,14 @@ def generate_test_object_graphs
7979
[Circle, Square, Triangle, NonPolyOne, NonPolyTwo].map(&:create!)
8080
end
8181
1.upto(NUM_SIMPLE_OBJS) do
82-
PaintColor.create!(:non_poly_one_id => NonPolyOne.rand.id)
83-
PaintTexture.create!(:non_poly_two_id => NonPolyTwo.rand.id)
82+
PaintColor.create!(:non_poly_one_id => NonPolyOne.random_element.id)
83+
PaintTexture.create!(:non_poly_two_id => NonPolyTwo.random_element.id)
8484
end
8585
1.upto(NUM_SHAPE_EXPRESSIONS) do
86-
shape_type = [Circle, Square, Triangle].rand
87-
paint_type = [PaintColor, PaintTexture].rand
88-
ShapeExpression.create!(:shape_type => shape_type.to_s, :shape_id => shape_type.rand.id,
89-
:paint_type => paint_type.to_s, :paint_id => paint_type.rand.id)
86+
shape_type = [Circle, Square, Triangle].random_element
87+
paint_type = [PaintColor, PaintTexture].random_element
88+
ShapeExpression.create!(:shape_type => shape_type.to_s, :shape_id => shape_type.random_element.id,
89+
:paint_type => paint_type.to_s, :paint_id => paint_type.random_element.id)
9090
end
9191
end
9292

activerecord/test/cases/named_scope_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def test_find_all_should_behave_like_select
301301
end
302302

303303
def test_rand_should_select_a_random_object_from_proxy
304-
assert_kind_of Topic, Topic.approved.rand
304+
assert_kind_of Topic, Topic.approved.random_element
305305
end
306306

307307
def test_should_use_where_in_query_for_named_scope
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
class Array
2+
# This method is deprecated because it masks Kernel#rand within the Array class itself,
3+
# which may be used by a 3rd party library extending Array in turn. See
4+
#
5+
# https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/4555
6+
#
7+
def rand # :nodoc:
8+
ActiveSupport::Deprecation.warn "Array#rand is deprecated, use random_element instead", caller
9+
random_element
10+
end
11+
212
# Returns a random element from the array.
3-
def rand
13+
def random_element
414
self[Kernel.rand(length)]
515
end
616
end

activesupport/test/core_ext/array_ext_test.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,15 +358,19 @@ def test_uniq_by!
358358
end
359359
end
360360

361-
class ArrayExtRandomTests < Test::Unit::TestCase
361+
class ArrayExtRandomTests < ActiveSupport::TestCase
362362
def test_random_element_from_array
363-
assert_nil [].rand
363+
assert_nil [].random_element
364364

365365
Kernel.expects(:rand).with(1).returns(0)
366-
assert_equal 'x', ['x'].rand
366+
assert_equal 'x', ['x'].random_element
367367

368368
Kernel.expects(:rand).with(3).returns(1)
369-
assert_equal 2, [1, 2, 3].rand
369+
assert_equal 2, [1, 2, 3].random_element
370+
end
371+
372+
def test_deprecated_rand_on_array
373+
assert_deprecated { [].rand }
370374
end
371375
end
372376

0 commit comments

Comments
 (0)