From c74045cf0771ab51dfeca94b30c447cab6193e60 Mon Sep 17 00:00:00 2001 From: Brian Christian Date: Tue, 9 Feb 2016 16:02:08 -0800 Subject: allow Array.penultimate and Array.antepenultiate access methods --- activesupport/lib/active_support/core_ext/array/access.rb | 14 ++++++++++++++ activesupport/test/core_ext/array/access_test.rb | 2 ++ 2 files changed, 16 insertions(+) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/core_ext/array/access.rb b/activesupport/lib/active_support/core_ext/array/access.rb index 3177d8498e..e413acddc5 100644 --- a/activesupport/lib/active_support/core_ext/array/access.rb +++ b/activesupport/lib/active_support/core_ext/array/access.rb @@ -73,4 +73,18 @@ class Array def forty_two self[41] end + + # Equal to self[-3]. + # + # %w( a b c d e ).antepenultimate # => "c" + def antepenultimate + self[-3] + end + + # Equal to self[-2]. + # + # %w( a b c d e ).penultimate # => "d" + def penultimate + self[-2] + end end diff --git a/activesupport/test/core_ext/array/access_test.rb b/activesupport/test/core_ext/array/access_test.rb index 3f1e0c4cb4..d7a3fb56c2 100644 --- a/activesupport/test/core_ext/array/access_test.rb +++ b/activesupport/test/core_ext/array/access_test.rb @@ -26,6 +26,8 @@ class AccessTest < ActiveSupport::TestCase assert_equal array[3], array.fourth assert_equal array[4], array.fifth assert_equal array[41], array.forty_two + assert_equal array[-3], array.antepenultimate + assert_equal array[-2], array.penultimate end def test_without -- cgit v1.2.3 From e8aeda2ab39b5d416a72edfa74e0fb17721eb6f9 Mon Sep 17 00:00:00 2001 From: Brian Christian Date: Wed, 10 Feb 2016 10:10:38 -0800 Subject: rename to 'second_to_last' and 'third_to_last' --- activesupport/lib/active_support/core_ext/array/access.rb | 8 ++++---- activesupport/test/core_ext/array/access_test.rb | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/core_ext/array/access.rb b/activesupport/lib/active_support/core_ext/array/access.rb index e413acddc5..37d833887a 100644 --- a/activesupport/lib/active_support/core_ext/array/access.rb +++ b/activesupport/lib/active_support/core_ext/array/access.rb @@ -76,15 +76,15 @@ class Array # Equal to self[-3]. # - # %w( a b c d e ).antepenultimate # => "c" - def antepenultimate + # %w( a b c d e ).third_to_last # => "c" + def third_to_last self[-3] end # Equal to self[-2]. # - # %w( a b c d e ).penultimate # => "d" - def penultimate + # %w( a b c d e ).second_to_last # => "d" + def second_to_last self[-2] end end diff --git a/activesupport/test/core_ext/array/access_test.rb b/activesupport/test/core_ext/array/access_test.rb index d7a3fb56c2..1d834667f0 100644 --- a/activesupport/test/core_ext/array/access_test.rb +++ b/activesupport/test/core_ext/array/access_test.rb @@ -26,8 +26,8 @@ class AccessTest < ActiveSupport::TestCase assert_equal array[3], array.fourth assert_equal array[4], array.fifth assert_equal array[41], array.forty_two - assert_equal array[-3], array.antepenultimate - assert_equal array[-2], array.penultimate + assert_equal array[-3], array.third_to_last + assert_equal array[-2], array.second_to_last end def test_without -- cgit v1.2.3 From eaa1efe7d0b4e0f5682453b1ddfa186a292ddc86 Mon Sep 17 00:00:00 2001 From: Brian Christian Date: Wed, 10 Feb 2016 10:31:44 -0800 Subject: include activerecord and activesupport CHANGELOG entries --- activesupport/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index bd333da081..3d4cc8fae6 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,7 @@ +* Add `Array#second_to_last` and `Array#third_to_last` methods. + + *Brian Christian* + * Fix regression in `Hash#dig` for HashWithIndifferentAccess. *Jon Moss* -- cgit v1.2.3