From 9482554f31f3ac7f941e6239890c60fcc01975e1 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 15 Aug 2011 16:56:33 -0500 Subject: Added Array#prepend as an alias for Array#unshift and Array#append as an alias for Array#<< [DHH] --- activesupport/CHANGELOG | 2 ++ activesupport/lib/active_support/core_ext/array.rb | 1 + .../lib/active_support/core_ext/array/prepend_and_append.rb | 7 +++++++ activesupport/test/core_ext/array_ext_test.rb | 10 ++++++++++ 4 files changed, 20 insertions(+) create mode 100644 activesupport/lib/active_support/core_ext/array/prepend_and_append.rb diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 2129e8b179..dba914da48 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *Rails 3.2.0 (unreleased)* +* Added Array#prepend as an alias for Array#unshift and Array#append as an alias for Array#<< [DHH] + * Removed support for using Module#delegate to delegate to non-public methods [Jon Leighton] * The definition of blank string for Ruby 1.9 has been extended to Unicode whitespace. diff --git a/activesupport/lib/active_support/core_ext/array.rb b/activesupport/lib/active_support/core_ext/array.rb index 4688468a8f..268c9bed4c 100644 --- a/activesupport/lib/active_support/core_ext/array.rb +++ b/activesupport/lib/active_support/core_ext/array.rb @@ -5,3 +5,4 @@ require 'active_support/core_ext/array/conversions' require 'active_support/core_ext/array/extract_options' require 'active_support/core_ext/array/grouping' require 'active_support/core_ext/array/random_access' +require 'active_support/core_ext/array/prepend_and_append' diff --git a/activesupport/lib/active_support/core_ext/array/prepend_and_append.rb b/activesupport/lib/active_support/core_ext/array/prepend_and_append.rb new file mode 100644 index 0000000000..27718f19d4 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/array/prepend_and_append.rb @@ -0,0 +1,7 @@ +class Array + # The human way of thinking about adding stuff to the end of a list is with append + alias_method :append, :<< + + # The human way of thinking about adding stuff to the beginning of a list is with prepend + alias_method :prepend, :unshift +end \ No newline at end of file diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb index e532010b18..f035505a01 100644 --- a/activesupport/test/core_ext/array_ext_test.rb +++ b/activesupport/test/core_ext/array_ext_test.rb @@ -465,3 +465,13 @@ class ArrayWrapperTests < Test::Unit::TestCase assert_equal DoubtfulToAry.new.to_ary, Array.wrap(DoubtfulToAry.new) end end + +class ArrayPrependAppendTest < Test::Unit::TestCase + def test_append + assert_equal [1, 2], [1].append(2) + end + + def test_prepend + assert_equal [2, 1], [1].prepend(2) + end +end \ No newline at end of file -- cgit v1.2.3