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

Ticket #8759: array_extract_options.3.diff

File array_extract_options.3.diff, 2.2 kB (added by norbert, 1 year ago)

... in a separate module

  • activesupport/test/core_ext/array_ext_test.rb

    old new  
    191191    assert xml.include?(%(<count>2</count>)), xml 
    192192  end 
    193193end 
     194 
     195class ArrayExtractOptionsTests < Test::Unit::TestCase 
     196  def test_extract_options 
     197    assert_equal({}, [].extract_options!) 
     198    assert_equal({}, [1].extract_options!) 
     199    assert_equal({:a=>:b}, [{:a=>:b}].extract_options!) 
     200    assert_equal({:a=>:b}, [1, {:a=>:b}].extract_options!) 
     201  end 
     202end 
  • activesupport/lib/active_support/core_ext/array.rb

    old new  
    11require File.dirname(__FILE__) + '/array/conversions' 
    22require File.dirname(__FILE__) + '/array/grouping' 
     3require File.dirname(__FILE__) + '/array/extract_options' 
    34 
    45class Array #:nodoc: 
    56  include ActiveSupport::CoreExtensions::Array::Conversions 
    67  include ActiveSupport::CoreExtensions::Array::Grouping 
     8  include ActiveSupport::CoreExtensions::Array::ExtractOptions 
    79end 
  • activesupport/lib/active_support/core_ext/array/extract_options.rb

    old new  
     1module ActiveSupport #:nodoc: 
     2  module CoreExtensions #:nodoc: 
     3    module Array #:nodoc: 
     4      module ExtractOptions 
     5        # Extract options from a set of arguments. Removes and returns the last element in the array if it's a hash, otherwise returns a blank hash. 
     6        # 
     7        #   def options(*args) 
     8        #     args.extract_options! 
     9        #   end 
     10        # 
     11        #   options(1, 2)           # => {} 
     12        #   options(1, 2, :a => :b) # => {:a=>:b} 
     13        def extract_options! 
     14          last.is_a?(::Hash) ? pop : {} 
     15        end 
     16      end 
     17    end 
     18  end 
     19end