Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 7486

Show
Ignore:
Timestamp:
09/15/07 21:39:04 (2 years ago)
Author:
david
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r7473 r7486  
    11*SVN* 
     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 
    28 
    39* Deprecation: removed Reloadable.  [Jeremy Kemper] 
  • trunk/activesupport/lib/active_support/core_ext/array.rb

    r7217 r7486  
    11require File.dirname(__FILE__) + '/array/conversions' 
     2require File.dirname(__FILE__) + '/array/extract_options' 
    23require File.dirname(__FILE__) + '/array/grouping' 
    3 require File.dirname(__FILE__) + '/array/extract_options' 
     4require File.dirname(__FILE__) + '/array/random_access' 
    45 
    56class Array #:nodoc: 
    67  include ActiveSupport::CoreExtensions::Array::Conversions 
     8  include ActiveSupport::CoreExtensions::Array::ExtractOptions 
    79  include ActiveSupport::CoreExtensions::Array::Grouping 
    8   include ActiveSupport::CoreExtensions::Array::ExtractOption
     10  include ActiveSupport::CoreExtensions::Array::RandomAcces
    911end 
  • trunk/activesupport/Rakefile

    r6881 r7486  
    1919  t.pattern = 'test/**/*_test.rb' 
    2020  t.verbose = true 
    21   t.warning = true 
    2221} 
    2322 
  • trunk/activesupport/test/abstract_unit.rb

    r4966 r7486  
    44require 'active_support' 
    55 
     6# Wrap tests that use Mocha and skip if unavailable. 
     7def uses_mocha(test_name) 
     8  require 'rubygems' 
     9  require 'mocha' 
     10  yield 
     11rescue LoadError 
     12  $stderr.puts "Skipping #{test_name} tests. `gem install mocha` and try again." 
     13end 
     14 
    615# Show backtraces for deprecated behavior for quicker cleanup. 
    716ActiveSupport::Deprecation.debug = true 
  • trunk/activesupport/test/core_ext/array_ext_test.rb

    r7474 r7486  
    201201  end 
    202202end 
     203 
     204uses_mocha "ArrayExtRandomTests" do 
     205 
     206class 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 
     216end 
     217 
     218end