[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

opposite of [thing].flatten

Phlip

7/25/2007 7:07:00 PM

Ruby-ists:

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?

--
Phlip
http://www.oreilly.com/catalog/9780...
^ assert_xpath
http://tinyurl.... <-- assert_raise_message
8 Answers

Philip Hallstrom

7/25/2007 7:27:00 PM

0

Bill Kelly

7/25/2007 7:32:00 PM

0


From: "Phlip" <phlip2005@gmail.com>
>
> 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


Regards,

Bill



Rob Biedenharn

7/25/2007 7:39:00 PM

0

On Jul 25, 2007, at 3:10 PM, Phlip wrote:
> Ruby-ists:
>
> 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?
>
> --
> Phlip
> http://www.oreilly.com/catalog/9780...
> ^ assert_xpath
> http://tinyurl.... <-- assert_raise_message

Except for the fact that it is deprecated, use .to_a:

irb(main):001:0> nil.to_a
=> []
irb(main):002:0> "one".to_a
=> ["one"]
irb(main):003:0> [1,2,3].to_a
=> [1, 2, 3]

------------------------------------------------------------ Object#to_a
obj.to_a -> anArray
------------------------------------------------------------------------
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.

self.to_a #=> -:1: warning: default `to_a' will be
obsolete
"hello".to_a #=> ["hello"]
Time.new.to_a #=> [39, 54, 8, 9, 4, 2003, 3, 99, true, "CDT"]


-Rob

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com




Gregory Brown

7/25/2007 7:55:00 PM

0

Not an answer to your question:

On 7/25/07, Phlip <phlip2005@gmail.com> wrote:
> Ruby-ists:
>
> 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

I think

thing = Array(thing)

is more idiomatic.

>> Array(nil)
=> []
>> Array("foo")
=> ["foo"]
>> Array(["foo"])
=> ["foo"]
>> Array([["foo"]])
=> [["foo"]]

It does not accidentally flatten nested arrays, and does not throw a
warning like Object#to_a does

Phlip

7/25/2007 8:53:00 PM

0

Bill Kelly wrote:

> 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

Ouch. I should'a knowed that!!! Wow. Ouch!

I knew Ruby could do it. I also figured I already knew the operator someone
would suggest.

(And extreme thanks for, uh, reading my question..? ;)

--
Phlip
http://www.oreilly.com/catalog/9780...
^ assert_xpath
http://tinyurl.... <-- assert_raise_message

Phlip

7/25/2007 9:16:00 PM

0

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!

--
Phlip
http://www.oreilly.com/catalog/9780...
^ assert_xpath
http://tinyurl.... <-- assert_raise_message

Gregory Brown

7/25/2007 9:52:00 PM

0

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!

You're right, but what you said is:

"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.

dblack

7/25/2007 9:57:00 PM

0