array.compact → an_array

Returns a copy of self with all nil elements removed.

   [ "a", nil, "b", nil, "c", nil ].compact
                     #=> [ "a", "b", "c" ]

on ruby 1.8.6

On 3/3/08, Arie Kusuma Atmaja <[EMAIL PROTECTED]> wrote:
> --- In [email protected], "Arie Kusuma Atmaja" <[EMAIL PROTECTED]>
>  wrote:
>
>  >
>  > --- In [email protected], Edwin Pratomo <edpratomo@> wrote:
>  > >
>  > >
>  > > --- Rie! <ariekusumaatmaja2@> wrote:
>  > >
>  > > > What i want to ask is more about language design
>  > > > cos I might've lost reading it from ruby-core
>  > > > mailing list and i've tried googling around but
>  > > > found nothing, what's the meaning of ruby compact
>  > > > actually if its output return result is just the
>  > > > same like the array before.
>  > > >
>  > > > ex.
>  > > >
>  > > > a = [ :a, :b, :c ]
>  > > > a << [ :a, :b ]
>  > > > => [ :a, :b, :c, [ :a, :b ] ]
>  > > > a.compact
>  > > > => [ :a, :b, :c, [ :a, :b ] ]
>  > > >
>  > >
>  > > lho .compact kan buat filter out nil values, ri?
>  > > (tiap kali lihat .compact jadi inget mr Kamal :-) hi there ! :-)
>  >
>  > here you go . even more fun ;-)
>  >
>  > m:~ arie$ irb19
>  > irb(main):001:0> a = [ :a, :b, :nil, :c ]
>  > => [:a, :b, :nil, :c]
>  > irb(main):002:0> a.compact
>  > => [:a, :b, :nil, :c]

symbol :nil bwt apa kk? mungkin nil, kalo gini:
irb(main):001:0> a = [:a, :b, nil, :c]
=> [:a, :b, nil, :c]
irb(main):002:0> a.compact
=> [:a, :b, :c]

>  > => [:a, :b, :nil, :c]
>  > irb(main):002:0> a.compact
>  > => [:a, :b, :nil, :c]
>  > irb(main):003:0> a << [ nil, nil, :a ]
>  > => [:a, :b, :nil, :c, [nil, nil, :a]]
>  > irb(main):004:0> a.compact
>  > => [:a, :b, :nil, :c, [nil, nil, :a]]
>  > irb(main):005:0>

kalo array [nil, nil, :a] ga kena compact-nya si array a.
:)

Kirim email ke