diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2016-02-10 20:45:12 +0100 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2016-02-10 20:45:12 +0100 |
commit | 074ff5ce147bdbc988b54dbc5fb60f6eae9506a9 (patch) | |
tree | 366baa1109a11d71f8ab7f1f34a5daa5e575d070 /activesupport | |
parent | 688996da7b25080a1a2ef74f5b4789f3e5eb670d (diff) | |
parent | eaa1efe7d0b4e0f5682453b1ddfa186a292ddc86 (diff) | |
download | rails-074ff5ce147bdbc988b54dbc5fb60f6eae9506a9.tar.gz rails-074ff5ce147bdbc988b54dbc5fb60f6eae9506a9.tar.bz2 rails-074ff5ce147bdbc988b54dbc5fb60f6eae9506a9.zip |
Merge pull request #23583 from brchristian/penultimate
Array.second_to_last and Array.third_to_last access methods
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG.md | 4 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/array/access.rb | 14 | ||||
-rw-r--r-- | activesupport/test/core_ext/array/access_test.rb | 2 |
3 files changed, 20 insertions, 0 deletions
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* diff --git a/activesupport/lib/active_support/core_ext/array/access.rb b/activesupport/lib/active_support/core_ext/array/access.rb index 3177d8498e..37d833887a 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 <tt>self[-3]</tt>. + # + # %w( a b c d e ).third_to_last # => "c" + def third_to_last + self[-3] + end + + # Equal to <tt>self[-2]</tt>. + # + # %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 3f1e0c4cb4..1d834667f0 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.third_to_last + assert_equal array[-2], array.second_to_last end def test_without |