You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
explains Array.wrap directly, rather by comparison with Kernel#Array which is too obscure, leaves the comparison to document the differences, and adds a comparison with the related idiom that uses the splat operator
Copy file name to clipboardExpand all lines: railties/guides/source/active_support_core_extensions.textile
+26-3Lines changed: 26 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -2214,14 +2214,27 @@ NOTE: Defined in +active_support/core_ext/array/conversions.rb+.
2214
2214
2215
2215
h4. Wrapping
2216
2216
2217
-
The class method +Array.wrap+ behaves like the function +Array()+ except:
2217
+
The method +Array.wrap+ wraps its argument in an array unless it is already an array (or array-like).
2218
+
2219
+
Specifically:
2220
+
2221
+
* If the argument is +nil+ an empty list is returned.
2222
+
* Otherwise, if the argument responds to +to_ary+ it is invoked, and its result returned.
2223
+
* Otherwise, returns an array with the argument as its single element.
2224
+
2225
+
<ruby>
2226
+
Array.wrap(nil) # => []
2227
+
Array.wrap([1, 2, 3]) # => [1, 2, 3]
2228
+
Array.wrap(0) # => [0]
2229
+
</ruby>
2230
+
2231
+
This method is similar in purpose to <tt>Kernel#Array</tt>, but there are some differences:
2218
2232
2219
2233
* If the argument responds to +to_ary+ the method is invoked. <tt>Kernel#Array</tt> moves on to try +to_a+ if the returned value is +nil+, but <tt>Arraw.wrap</tt> returns such a +nil+ right away.
2220
2234
* If the returned value from +to_ary+ is neither +nil+ nor an +Array+ object, <tt>Kernel#Array</tt> raises an exception, while <tt>Array.wrap</tt> does not, it just returns the value.
2221
2235
* It does not call +to_a+ on the argument, though special-cases +nil+ to return an empty array.
2222
2236
2223
-
2224
-
that it does not try to call +to_a+ on its argument. That changes the behavior for enumerables:
2237
+
The last point is particularly worth comparing for some enumerables:
0 commit comments