Skip to content
This repository
Browse code

Added Array#rand (closes #9170) [norbert]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7486 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 3f29043f456af423c4ec7599040625c87321aa77 1 parent 0b0931e
David Heinemeier Hansson authored
6  activesupport/CHANGELOG
... ...
@@ -1,5 +1,11 @@
1 1
 *SVN*
2 2
 
  3
+* Added Array#rand #9170 [norbert]. Examples:
  4
+
  5
+    [].rand       # => nil
  6
+    ['a'].rand    # => 'a'
  7
+    [1,2,3].rand  # => 1 or 2 or 3
  8
+
3 9
 * Deprecation: removed Reloadable.  [Jeremy Kemper]
4 10
 
5 11
 * Make the utf-handler return the correct value for non-matching regular expressions. Closes #9049 [manfred]
1  activesupport/Rakefile
@@ -18,7 +18,6 @@ task :default => :test
18 18
 Rake::TestTask.new { |t| 
19 19
   t.pattern = 'test/**/*_test.rb'
20 20
   t.verbose = true
21  
-  t.warning = true
22 21
 }
23 22
 
24 23
 # Create compressed packages
6  activesupport/lib/active_support/core_ext/array.rb
... ...
@@ -1,9 +1,11 @@
1 1
 require File.dirname(__FILE__) + '/array/conversions'
2  
-require File.dirname(__FILE__) + '/array/grouping'
3 2
 require File.dirname(__FILE__) + '/array/extract_options'
  3
+require File.dirname(__FILE__) + '/array/grouping'
  4
+require File.dirname(__FILE__) + '/array/random_access'
4 5
 
5 6
 class Array #:nodoc:
6 7
   include ActiveSupport::CoreExtensions::Array::Conversions
7  
-  include ActiveSupport::CoreExtensions::Array::Grouping
8 8
   include ActiveSupport::CoreExtensions::Array::ExtractOptions
  9
+  include ActiveSupport::CoreExtensions::Array::Grouping
  10
+  include ActiveSupport::CoreExtensions::Array::RandomAccess
9 11
 end
9  activesupport/test/abstract_unit.rb
@@ -3,5 +3,14 @@
3 3
 $:.unshift "#{File.dirname(__FILE__)}/../lib"
4 4
 require 'active_support'
5 5
 
  6
+# Wrap tests that use Mocha and skip if unavailable.
  7
+def uses_mocha(test_name)
  8
+  require 'rubygems'
  9
+  require 'mocha'
  10
+  yield
  11
+rescue LoadError
  12
+  $stderr.puts "Skipping #{test_name} tests. `gem install mocha` and try again."
  13
+end
  14
+
6 15
 # Show backtraces for deprecated behavior for quicker cleanup.
7 16
 ActiveSupport::Deprecation.debug = true
16  activesupport/test/core_ext/array_ext_test.rb
@@ -200,3 +200,19 @@ def test_extract_options
200 200
     assert_equal({:a=>:b}, [1, {:a=>:b}].extract_options!)
201 201
   end
202 202
 end
  203
+
  204
+uses_mocha "ArrayExtRandomTests" do
  205
+
  206
+class ArrayExtRandomTests < Test::Unit::TestCase
  207
+  def test_random_element_from_array
  208
+    assert_nil [].rand
  209
+
  210
+    Kernel.expects(:rand).with(1).returns(0)
  211
+    assert_equal 'x', ['x'].rand
  212
+
  213
+    Kernel.expects(:rand).with(3).returns(1)
  214
+    assert_equal 2, [1, 2, 3].rand
  215
+  end
  216
+end
  217
+
  218
+end

0 notes on commit 3f29043

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