Permalink
Browse files

removes Array#random_element and backports Array#sample from Ruby 1.9…

…, thanks to Marc-Andre Lafortune
  • Loading branch information...
1 parent 6401ab5 commit 67a43554f153a3ddb97039b5fac305c0619dd372 @fxn fxn committed Jun 4, 2010
@@ -17,7 +17,7 @@ def remember; self.class.remembered << self; end
module ClassMethods
def remembered; @@remembered ||= []; end
- def random_element; @@remembered.random_element; end
+ def sample; @@remembered.sample; end
end
end
@@ -79,14 +79,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.random_element.id)
- PaintTexture.create!(:non_poly_two_id => NonPolyTwo.random_element.id)
+ PaintColor.create!(:non_poly_one_id => NonPolyOne.sample.id)
+ PaintTexture.create!(:non_poly_two_id => NonPolyTwo.sample.id)
end
1.upto(NUM_SHAPE_EXPRESSIONS) do
- 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)
+ shape_type = [Circle, Square, Triangle].sample
+ paint_type = [PaintColor, PaintTexture].sample
+ ShapeExpression.create!(:shape_type => shape_type.to_s, :shape_id => shape_type.sample.id,
+ :paint_type => paint_type.to_s, :paint_id => paint_type.sample.id)
end
end
@@ -301,7 +301,7 @@ def test_find_all_should_behave_like_select
end
def test_rand_should_select_a_random_object_from_proxy
- assert_kind_of Topic, Topic.approved.random_element
+ assert_kind_of Topic, Topic.approved.sample
end
def test_should_use_where_in_query_for_named_scope
@@ -4,7 +4,7 @@
* Ruby 1.9: support UTF-8 case folding. #4595 [Norman Clarke]
-* Renames Array#rand -> Array#random_element. [Santiago Pastorino, Rizwan Reza]
+* Removes Array#rand and backports Array#sample from Ruby 1.9, thanks to Marc-Andre Lafortune. [fxn]
* Ruby 1.9: Renames last_(month|year) to prev_(month|year) in Date and Time. [fxn]
@@ -1,6 +1,20 @@
class Array
- # Returns a random element from the array.
- def random_element
- self[Kernel.rand(length)]
- end
+ # Backport of Array#sample based on Marc-Andre Lafortune's http://github.com/marcandre/backports/
+ 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
@@ -359,14 +359,30 @@ def test_uniq_by!
end
class ArrayExtRandomTests < ActiveSupport::TestCase
- def test_random_element_from_array
- assert_nil [].random_element
-
- Kernel.expects(:rand).with(1).returns(0)
- assert_equal 'x', ['x'].random_element
+ 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) }
- Kernel.expects(:rand).with(3).returns(1)
- assert_equal 2, [1, 2, 3].random_element
+ assert_raises(ArgumentError) { [0].sample(-7) }
end
end
@@ -512,6 +512,7 @@ These are the main changes in Active Support:
* Active Support no longer provides vendored versions of "TZInfo":http://tzinfo.rubyforge.org/, "Memcache Client":http://deveiate.org/projects/RMemCache/ and "Builder":http://builder.rubyforge.org/, these are all included as dependencies and installed via the <tt>bundle install</tt> command.
* Safe buffers are implemented in <tt>ActiveSupport::SafeBuffer</tt>.
* Added <tt>Array.uniq_by</tt> and <tt>Array.uniq_by!</tt>.
+* Removed <tt>Array#rand</tt> and backported <tt>Array#sample</tt> from Ruby 1.9.
* Fixed bug on +TimeZone.seconds_to_utc_offset+ returning wrong value.
* Added <tt>ActiveSupport::Notifications</tt> middleware.
* <tt>ActiveSupport.use_standard_json_time_format</tt> now defaults to true.
@@ -1927,13 +1927,23 @@ Similarly, +from+ returns the tail from the element at the passed index on:
The methods +second+, +third+, +fourth+, and +fifth+ return the corresponding element (+first+ is builtin). Thanks to social wisdom and positive constructiveness all around, +forty_two+ is also available.
-You can pick a random element with +random_element+:
+NOTE: Defined in +active_support/core_ext/array/access.rb+.
+
+h4. Random Access
+
+Active Support backports +sample+ from Ruby 1.9:
+
+You can pick a random element with +sample+:
<ruby>
-shape_type = [Circle, Square, Triangle].random_element
+shape_type = [Circle, Square, Triangle].sample
+# => Square, for example
+
+shape_types = [Circle, Square, Triangle].sample(2)
+# => [Triangle, Circle], for example
</ruby>
-NOTE: Defined in +active_support/core_ext/array/access.rb+.
+NOTE: Defined in +active_support/core_ext/array/random_access.rb+.
h4. Options Extraction
@@ -47,7 +47,7 @@ def setup
test "if there's no config.active_support.bare, all of ActiveSupport is required" do
use_frameworks []
require "#{app_path}/config/environment"
- assert_nothing_raised { [1,2,3].random_element }
+ assert_nothing_raised { [1,2,3].sample }
end
test "config.active_support.bare does not require all of ActiveSupport" do
@@ -57,7 +57,7 @@ def setup
Dir.chdir("#{app_path}/app") do
require "#{app_path}/config/environment"
- assert_raises(NoMethodError) { [1,2,3].random_element }
+ assert_raises(NoMethodError) { [1,2,3].sample }
end
end

0 comments on commit 67a4355

Please sign in to comment.