Ticket #8759: array_extract_options.diff
| File array_extract_options.diff, 1.7 kB (added by norbert, 2 years 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 ArrayTests < Test::Unit::TestCase 196 def test_extract_options 197 assert_equal({}, [].extract_options!) 198 assert_equal({}, [1].extract_options!) 199 assert_equal({:a=>:b}, [1, {:a=>:b}].extract_options!) 200 end 201 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/misc' 3 4 4 5 class Array #:nodoc: 5 6 include ActiveSupport::CoreExtensions::Array::Conversions -
activesupport/lib/active_support/core_ext/array/misc.rb
old new 1 class Array 2 # 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. 3 # 4 # def options(*args) 5 # args.extract_options! 6 # end 7 # 8 # options(1, 2) # => {} 9 # options(1, 2, :a => :b) # => {:a=>:b} 10 def extract_options! 11 self.last.is_a?(Hash) ? pop : {} 12 end 13 end