[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Replacement idiom for "list_or_nil.to_a"?

Farrel Lifson

5/25/2007 10:35:00 AM

On 25/05/07, Rich Morin <rdm@cfcl.com> wrote:
> I suppose I could define my own to_a method for nil,
> but that seems a bit questionable, as well. So, is
> there a Better Way To Do It?

If you're going to get a list or nil then I find it nice to wrap it in
the Kernel#Array() method. It will convert a nil to a an empty array
but will leave an array untouched:

irb(main):001:0> Array(nil)
=> []
irb(main):002:0> Array([1,2,3])
=> [1, 2, 3]

Farrel