Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Array#wrap should use #to_ary so association collections and named sc…

…opes are not re-wrapped

[#1935 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
commit 1a2a71333f197e98d1cd7fe380d613cbada4446b 1 parent ff3fb6c
@willbryant willbryant authored jeremy committed
View
9 activesupport/lib/active_support/core_ext/array/wrapper.rb
@@ -2,7 +2,8 @@ module ActiveSupport #:nodoc:
module CoreExtensions #:nodoc:
module Array #:nodoc:
module Wrapper
- # Wraps the object in an Array unless it's an Array.
+ # Wraps the object in an Array unless it's an Array. Converts the
+ # object to an Array using #to_ary if it implements that.
def wrap(object)
case object
when nil
@@ -10,7 +11,11 @@ def wrap(object)
when self
object
else
- [object]
+ if object.respond_to?(:to_ary)
+ object.to_ary
+ else
+ [object]
+ end
end
end
end
View
10 activesupport/test/core_ext/array_ext_test.rb
@@ -304,6 +304,12 @@ def test_random_element_from_array
end
class ArrayWrapperTests < Test::Unit::TestCase
+ class FakeCollection
+ def to_ary
+ ["foo", "bar"]
+ end
+ end
+
def test_array
ary = %w(foo bar)
assert_same ary, Array.wrap(ary)
@@ -325,4 +331,8 @@ def test_string
def test_string_with_newline
assert_equal ["foo\nbar"], Array.wrap("foo\nbar")
end
+
+ def test_object_with_to_ary
+ assert_equal ["foo", "bar"], Array.wrap(FakeCollection.new)
+ end
end

0 comments on commit 1a2a713

Please sign in to comment.
Something went wrong with that request. Please try again.