aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2009-02-06 10:04:43 -0800
committerJeremy Kemper <jeremy@bitsweat.net>2009-02-06 10:04:43 -0800
commit676b0c87642786080ab9e22fcc86a13d15324d4b (patch)
tree8e03242f8b67e17a4ebd8630e2ab7dc194037867 /activesupport
parent7564d98929af0bb039cb10c59dff6d177c11bd13 (diff)
downloadrails-676b0c87642786080ab9e22fcc86a13d15324d4b.tar.gz
rails-676b0c87642786080ab9e22fcc86a13d15324d4b.tar.bz2
rails-676b0c87642786080ab9e22fcc86a13d15324d4b.zip
Introduce Array.wrap(foo) to wrap the argument in an array unless it's already an array. Wraps nil as an empty array. Use instead of Array(foo) and foo.to_a since they treat String as Enumerable.
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG5
-rw-r--r--activesupport/lib/active_support/core_ext/array.rb2
-rw-r--r--activesupport/lib/active_support/core_ext/array/wrapper.rb19
-rw-r--r--activesupport/test/core_ext/array_ext_test.rb24
4 files changed, 50 insertions, 0 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index 24e0836bde..4197d5d0be 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,3 +1,8 @@
+*Edge*
+
+* Introduce Array.wrap(foo) to wrap the argument in an array unless it's already an array. Wraps nil as an empty array. Use instead of Array(foo) and foo.to_a since they treat String as Enumerable. [Jeremy Kemper]
+
+
*2.3.0 [RC1] (February 1st, 2009)*
* TimeWithZone#xmlschema accepts optional fraction_digits argument [#1725 state:resolved] [Nicholas Dainty]
diff --git a/activesupport/lib/active_support/core_ext/array.rb b/activesupport/lib/active_support/core_ext/array.rb
index cc0a1ebc12..82c6b1243a 100644
--- a/activesupport/lib/active_support/core_ext/array.rb
+++ b/activesupport/lib/active_support/core_ext/array.rb
@@ -3,6 +3,7 @@ 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/wrapper'
class Array #:nodoc:
include ActiveSupport::CoreExtensions::Array::Access
@@ -10,4 +11,5 @@ class Array #:nodoc:
include ActiveSupport::CoreExtensions::Array::ExtractOptions
include ActiveSupport::CoreExtensions::Array::Grouping
include ActiveSupport::CoreExtensions::Array::RandomAccess
+ extend ActiveSupport::CoreExtensions::Array::Wrapper
end
diff --git a/activesupport/lib/active_support/core_ext/array/wrapper.rb b/activesupport/lib/active_support/core_ext/array/wrapper.rb
new file mode 100644
index 0000000000..12fd745b1a
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/array/wrapper.rb
@@ -0,0 +1,19 @@
+module ActiveSupport #:nodoc:
+ module CoreExtensions #:nodoc:
+ module Array #:nodoc:
+ module Wrapper
+ # Wraps the object in an Array unless it's an Array.
+ def wrap(object)
+ case object
+ when nil
+ []
+ when self
+ object
+ else
+ [object]
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb
index 93f4482307..a90d6891cf 100644
--- a/activesupport/test/core_ext/array_ext_test.rb
+++ b/activesupport/test/core_ext/array_ext_test.rb
@@ -302,3 +302,27 @@ class ArrayExtRandomTests < Test::Unit::TestCase
assert_equal 2, [1, 2, 3].rand
end
end
+
+class ArrayWrapperTests < Test::Unit::TestCase
+ def test_array
+ ary = %w(foo bar)
+ assert_same ary, Array.wrap(ary)
+ end
+
+ def test_nil
+ assert_equal [], Array.wrap(nil)
+ end
+
+ def test_object
+ o = Object.new
+ assert_equal [o], Array.wrap(o)
+ end
+
+ def test_string
+ assert_equal ["foo"], Array.wrap("foo")
+ end
+
+ def test_string_with_newline
+ assert_equal ["foo\nbar"], Array.wrap("foo\nbar")
+ end
+end