We all know this clever idiom to turn a variable that might be an Array into
one known to be an Array:
thing = [thing].flatten
That saves a lot of if statements to permit thing's type to overload.
What is the opposite (clever) operation? How to turn a list of one item into
one item, and a list of zero items into nil, but pass thru the list of many
items?
We all know this clever idiom to turn a variable that might be an Array into
one known to be an Array:
thing = [thing].flatten
That saves a lot of if statements to permit thing's type to overload.
What is the opposite (clever) operation? How to turn a list of one item into
one item, and a list of zero items into nil, but pass thru the list of many
items?
We all know this clever idiom to turn a variable that might be an Array into
one known to be an Array:
thing = [thing].flatten
That saves a lot of if statements to permit thing's type to overload.
What is the opposite (clever) operation? How to turn a list of one item into
one item, and a list of zero items into nil, but pass thru the list of many
items?
Maybe:
irb(main):065:0> x = *[1,2,3]
=> [1, 2, 3]
irb(main):066:0> x = *[1]
=> 1
irb(main):067:0> x = *
=> nil
We all know this clever idiom to turn a variable that might be an Array into
one known to be an Array:
thing = [thing].flatten
That saves a lot of if statements to permit thing's type to overload.
What is the opposite (clever) operation? How to turn a list of one item into
one item, and a list of zero items into nil, but pass thru the list of many
items?
------------------------------------------------------------------------
Returns an array representation of _obj_. For objects of class
+Object+ and others that don't explicitly override the method, the
return value is an array containing +self+. However, this latter
behavior will soon be obsolete.
It does not accidentally flatten nested arrays, and does not throw a
warning like Object#to_a does
Ruby is a strongly-typed language that permits you to write interfaces that
are as feebly- or strongly-typed (or statically-typed) as you like.
If your interface permits one item or a series of similar items, then those
nested arrays are not "accidentally" flattened, they are deliberately
pushed into your interface's contract.
If flattening that deep array and traversing its elements surprises the
user, then maybe they shouldn't pass arrays in that they convoluted for
some other reason!
"We all know this clever idiom to turn a variable that might be an Array into
one known to be an Array:"
That's not what your 'idiom' does. It flattens an array that you
create from a single object. Careful wording is important.
···
On 7/25/07, Phlip <phlip2005@gmail.com> wrote:
Gregory Brown wrote:
>>> Array([["foo"]])
> => [["foo"]]
>
> It does not accidentally flatten nested arrays, and does not throw a
> warning like Object#to_a does
Ruby is a strongly-typed language that permits you to write interfaces that
are as feebly- or strongly-typed (or statically-typed) as you like.
If your interface permits one item or a series of similar items, then those
nested arrays are not "accidentally" flattened, they are deliberately
pushed into your interface's contract.
If flattening that deep array and traversing its elements surprises the
user, then maybe they shouldn't pass arrays in that they convoluted for
some other reason!
It does not accidentally flatten nested arrays, and does not throw a
warning like Object#to_a does
Ruby is a strongly-typed language that permits you to write interfaces that
are as feebly- or strongly-typed (or statically-typed) as you like.
If your interface permits one item or a series of similar items, then those
nested arrays are not "accidentally" flattened, they are deliberately
pushed into your interface's contract.
There are certainly cases where the flattening is not accidental, but
it sounds like those aren't the ones Greg was referring to It's
really just a question of flattening being greedy and a bit of a
"gotcha" at times. In any case, 1.9 introduces an argument to
flatten, so one can do: