Ticket #8759: array_extract_options.3.diff
| File array_extract_options.3.diff, 2.2 kB (added by norbert, 1 year ago) |
|---|
-
activesupport/test/core_ext/array_ext_test.rb
old new 191 191 assert xml.include?(%(<count>2</count>)), xml 192 192 end 193 193 end 194 195 class 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 202 end -
activesupport/lib/active_support/core_ext/array.rb
old new 1 1 require File.dirname(__FILE__) + '/array/conversions' 2 2 require File.dirname(__FILE__) + '/array/grouping' 3 require File.dirname(__FILE__) + '/array/extract_options' 3 4 4 5 class Array #:nodoc: 5 6 include ActiveSupport::CoreExtensions::Array::Conversions 6 7 include ActiveSupport::CoreExtensions::Array::Grouping 8 include ActiveSupport::CoreExtensions::Array::ExtractOptions 7 9 end -
activesupport/lib/active_support/core_ext/array/extract_options.rb
old new 1 module 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 19 end