diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 50045a8c17b652b4ca64186214913057a4149cc6..7eaad6340f064f69cbb522d6ad76ad87bb9e232b 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,4 +1,4 @@ -* Added `ActiveSupport::ArrayInquirer`. +* Added `ActiveSupport::ArrayInquirer` and `Array#inquiry`. Wrapping an array in an `ArrayInquirer` gives a friendlier way to check its contents: @@ -13,6 +13,9 @@ variants.any?(:phone, :desktop) # => true variants.any?(:desktop, :watch) # => false + `Array#inquiry` is a shortcut for wrapping the receiving array in an + `ArrayInquirer`. + *George Claghorn* * Deprecate `alias_method_chain` in favour of `Module#prepend` introduced in Ruby 2.0 diff --git a/activesupport/lib/active_support/core_ext/array.rb b/activesupport/lib/active_support/core_ext/array.rb index 7d0c1e4c8d5c9d00325ea4151de8b7fa23f7aa59..7551551bd73216a0adf5238ed58651b56f84efc3 100644 --- a/activesupport/lib/active_support/core_ext/array.rb +++ b/activesupport/lib/active_support/core_ext/array.rb @@ -4,3 +4,4 @@ require 'active_support/core_ext/array/extract_options' require 'active_support/core_ext/array/grouping' require 'active_support/core_ext/array/prepend_and_append' +require 'active_support/core_ext/array/inquiry' diff --git a/activesupport/lib/active_support/core_ext/array/inquiry.rb b/activesupport/lib/active_support/core_ext/array/inquiry.rb new file mode 100644 index 0000000000000000000000000000000000000000..de623c466cb8df86d6d99fc087744b59943f2d39 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/array/inquiry.rb @@ -0,0 +1,15 @@ +class Array + # Wraps the array in an +ArrayInquirer+ object, which gives a friendlier way + # to check its string-like contents. + # + # pets = [:cat, :dog].inquiry + # + # pets.cat? # => true + # pets.ferret? # => false + # + # pets.any?(:cat, :ferret) # => true + # pets.any?(:ferret, :alligator) # => false + def inquiry + ActiveSupport::ArrayInquirer.new(self) + end +end diff --git a/activesupport/test/array_inquirer_test.rb b/activesupport/test/array_inquirer_test.rb index 97adea85e63584758507f6e050cca3505c1364d5..488e0d34a9e5cb10b6f6ee7243f118a028f78e99 100644 --- a/activesupport/test/array_inquirer_test.rb +++ b/activesupport/test/array_inquirer_test.rb @@ -25,4 +25,11 @@ def test_any_with_block def test_respond_to assert_respond_to @array_inquirer, :development? end + + def test_inquiry + result = [:mobile, :tablet].inquiry + + assert_instance_of ActiveSupport::ArrayInquirer, result + assert_equal @array_inquirer, result + end end