Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Deprecate Array#rand in favor of Array#random_element [#4555 stated:c…

…ommitted]

Signed-off-by: Xavier Noria <fxn@hashref.com>
  • Loading branch information...
commit 32b0b5f7b25a05179981d97d6b47aa4a1c683f2f 1 parent 76608b1
@rizwanreza rizwanreza authored fxn committed
View
12 activerecord/test/cases/associations/eager_load_nested_include_test.rb
@@ -81,14 +81,14 @@ def generate_test_object_graphs
[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
View
2  activerecord/test/cases/named_scope_test.rb
@@ -265,7 +265,7 @@ def test_find_all_should_behave_like_select
end
def test_rand_should_select_a_random_object_from_proxy
- assert Topic.approved.rand.is_a?(Topic)
+ assert Topic.approved.random_element.is_a?(Topic)
end
def test_should_use_where_in_query_for_named_scope
View
2  activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*2.3.6 (pending)*
+* Deprecated Array#rand in favor of Array#random_element. [Santiago Pastorino, Rizwan Reza]
+
* Added Object#presence that returns the object if it's #present? otherwise returns nil [DHH/Colin Kelley]
* New assertions assert_blank and assert_present. #4299 [Juanjo Bazan]
View
12 activesupport/lib/active_support/core_ext/array/random_access.rb
@@ -2,8 +2,18 @@ module ActiveSupport #:nodoc:
module CoreExtensions #:nodoc:
module Array #:nodoc:
module RandomAccess
+ # 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 and will be removed in Rails 3. 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
View
12 activesupport/test/core_ext/array_ext_test.rb
@@ -323,15 +323,19 @@ def test_extract_options
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
Please sign in to comment.
Something went wrong with that request. Please try again.