Permalink
Browse files

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...
1 parent 0b0931e commit 3f29043f456af423c4ec7599040625c87321aa77 @dhh dhh committed Sep 15, 2007
@@ -1,5 +1,11 @@
*SVN*
+* Added Array#rand #9170 [norbert]. Examples:
+
+ [].rand # => nil
+ ['a'].rand # => 'a'
+ [1,2,3].rand # => 1 or 2 or 3
+
* Deprecation: removed Reloadable. [Jeremy Kemper]
* Make the utf-handler return the correct value for non-matching regular expressions. Closes #9049 [manfred]
@@ -18,7 +18,6 @@ task :default => :test
Rake::TestTask.new { |t|
t.pattern = 'test/**/*_test.rb'
t.verbose = true
- t.warning = true
}
# Create compressed packages
@@ -1,9 +1,11 @@
require File.dirname(__FILE__) + '/array/conversions'
-require File.dirname(__FILE__) + '/array/grouping'
require File.dirname(__FILE__) + '/array/extract_options'
+require File.dirname(__FILE__) + '/array/grouping'
+require File.dirname(__FILE__) + '/array/random_access'
class Array #:nodoc:
include ActiveSupport::CoreExtensions::Array::Conversions
- include ActiveSupport::CoreExtensions::Array::Grouping
include ActiveSupport::CoreExtensions::Array::ExtractOptions
+ include ActiveSupport::CoreExtensions::Array::Grouping
+ include ActiveSupport::CoreExtensions::Array::RandomAccess
end
@@ -3,5 +3,14 @@
$:.unshift "#{File.dirname(__FILE__)}/../lib"
require 'active_support'
+# Wrap tests that use Mocha and skip if unavailable.
+def uses_mocha(test_name)
+ require 'rubygems'
+ require 'mocha'
+ yield
+rescue LoadError
+ $stderr.puts "Skipping #{test_name} tests. `gem install mocha` and try again."
+end
+
# Show backtraces for deprecated behavior for quicker cleanup.
ActiveSupport::Deprecation.debug = true
@@ -200,3 +200,19 @@ def test_extract_options
assert_equal({:a=>:b}, [1, {:a=>:b}].extract_options!)
end
end
+
+uses_mocha "ArrayExtRandomTests" do
+
+class ArrayExtRandomTests < Test::Unit::TestCase
+ def test_random_element_from_array
+ assert_nil [].rand
+
+ Kernel.expects(:rand).with(1).returns(0)
+ assert_equal 'x', ['x'].rand
+
+ Kernel.expects(:rand).with(3).returns(1)
+ assert_equal 2, [1, 2, 3].rand
+ end
+end
+
+end

0 comments on commit 3f29043

Please sign in to comment.