mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Change on Array extension from rand => random_element [#4555 state:committed]
Signed-off-by: Xavier Noria <fxn@hashref.com>
This commit is contained in:
parent
64d109e353
commit
821e15e5f2
4 changed files with 27 additions and 13 deletions
|
@ -17,7 +17,7 @@ module Remembered
|
|||
|
||||
module ClassMethods
|
||||
def remembered; @@remembered ||= []; end
|
||||
def rand; @@remembered.rand; end
|
||||
def random_element; @@remembered.random_element; end
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -79,14 +79,14 @@ class EagerLoadPolyAssocsTest < ActiveRecord::TestCase
|
|||
[Circle, Square, Triangle, NonPolyOne, NonPolyTwo].map(&:create!)
|
||||
end
|
||||
1.upto(NUM_SIMPLE_OBJS) do
|
||||
PaintColor.create!(:non_poly_one_id => NonPolyOne.rand.id)
|
||||
PaintTexture.create!(:non_poly_two_id => NonPolyTwo.rand.id)
|
||||
PaintColor.create!(:non_poly_one_id => NonPolyOne.random_element.id)
|
||||
PaintTexture.create!(:non_poly_two_id => NonPolyTwo.random_element.id)
|
||||
end
|
||||
1.upto(NUM_SHAPE_EXPRESSIONS) do
|
||||
shape_type = [Circle, Square, Triangle].rand
|
||||
paint_type = [PaintColor, PaintTexture].rand
|
||||
ShapeExpression.create!(:shape_type => shape_type.to_s, :shape_id => shape_type.rand.id,
|
||||
:paint_type => paint_type.to_s, :paint_id => paint_type.rand.id)
|
||||
shape_type = [Circle, Square, Triangle].random_element
|
||||
paint_type = [PaintColor, PaintTexture].random_element
|
||||
ShapeExpression.create!(:shape_type => shape_type.to_s, :shape_id => shape_type.random_element.id,
|
||||
:paint_type => paint_type.to_s, :paint_id => paint_type.random_element.id)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -301,7 +301,7 @@ class NamedScopeTest < ActiveRecord::TestCase
|
|||
end
|
||||
|
||||
def test_rand_should_select_a_random_object_from_proxy
|
||||
assert_kind_of Topic, Topic.approved.rand
|
||||
assert_kind_of Topic, Topic.approved.random_element
|
||||
end
|
||||
|
||||
def test_should_use_where_in_query_for_named_scope
|
||||
|
|
|
@ -1,6 +1,16 @@
|
|||
class Array
|
||||
# This method is deprecated because it masks Kernel#rand within the Array class itself,
|
||||
# which may be used by a 3rd party library extending Array in turn. See
|
||||
#
|
||||
# https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/4555
|
||||
#
|
||||
def rand # :nodoc:
|
||||
ActiveSupport::Deprecation.warn "Array#rand is deprecated, use random_element instead", caller
|
||||
random_element
|
||||
end
|
||||
|
||||
# Returns a random element from the array.
|
||||
def rand
|
||||
def random_element
|
||||
self[Kernel.rand(length)]
|
||||
end
|
||||
end
|
||||
|
|
|
@ -358,15 +358,19 @@ class ArrayUniqByTests < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
class ArrayExtRandomTests < Test::Unit::TestCase
|
||||
class ArrayExtRandomTests < ActiveSupport::TestCase
|
||||
def test_random_element_from_array
|
||||
assert_nil [].rand
|
||||
assert_nil [].random_element
|
||||
|
||||
Kernel.expects(:rand).with(1).returns(0)
|
||||
assert_equal 'x', ['x'].rand
|
||||
assert_equal 'x', ['x'].random_element
|
||||
|
||||
Kernel.expects(:rand).with(3).returns(1)
|
||||
assert_equal 2, [1, 2, 3].rand
|
||||
assert_equal 2, [1, 2, 3].random_element
|
||||
end
|
||||
|
||||
def test_deprecated_rand_on_array
|
||||
assert_deprecated { [].rand }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue