I’ve found this useful in situations where I know I’ll get a one-element array, and I just want the element, such as an api request that always returns an array even though I asked for a single result, or when I’m not sure I’ll get an array or single element (the same situation as Array.wrap except I want the single element of an array containing that element).
Anyway, I’m looking for feedback. Does anyone have thoughts before I make this a pull-request?
I've found this useful in situations where I know I'll get a one-element array, and I just want the element, such as an api request that always returns an array even though I asked for a single result, or when I'm not sure I'll get an array or single element
I think, sadly, I have to -1 on this one. It does not clear when I see this method what would happen when you pass in array with multiple elements, or when the array unexpextedly have moret than one element. Assume that that array will always has 1 element is … Not a good idea.
I think it’s already clear and make sense to just call .first or [0] on it. Since you would be like “I don’t care how many things it has in it, I want the first one.”
Not meaning to be a thread necromancer here, but I literally am in a pairing session right now where I said “I really want a method that does the inverse of Array.wrap, like Array.unwrap…wait a minute, does it exist already” and then I found this thread.
Your original code change is long gone, but I wanted to revive this discussion, on these grounds:
Array.wrap is new Array behavior that is introduced in ActiveSupport; therefore an enhancement like this would also belong in ActiveSupport
Array.wrap provides a new simple behavior for Array
Array.unwrap would provide inverse-parity with Array.wrap and is named in a fashion that suggests the behavior intuitively, if you already know the behavior of .wrap (bonus if we can have an instance-level method, Array#wrap)
It puts all the parameters into individual arrays which is fine as a default behavior, but like the OP, we wanted the elements to be singular strings instead of arrays. So something like this:
a = Array.wrap('b')
=> ['b']
Array.unwrap(a)
=> 'b'
Anyways, no idea if it’s possible to add this still, but I would also love to see it and wanted OP to know they weren’t alone in thinking of this. GMTA!