From: Intransition Date: 2012-06-25T08:35:19+09:00 Subject: Enumerable#unassociate ------=_Part_275_27629376.1340580916814 Content-Type: multipart/alternative; boundary="----=_Part_276_7154744.1340580916814" ------=_Part_276_7154744.1340580916814 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Have a look: module Enumerable # Take an associative array and unassociate it. # # [[:a,1], [:b,2]].unassociate.to_a #=> [:a, [1], :b, [2]] # [[:a,1], [:b,2]].unassociate(1).to_a #=> [:a, 1, :b, 2] # def unassociate(index = 1..-1) return to_enum(:unassociate, index) unless block_given? each do |v| case v when Array yield v[0] yield v[index] else yield v yield nil end end end end Is it a good method name? Is 1..-1 the best default? Other thoughts/ suggestions? ------=_Part_276_7154744.1340580916814 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
Have a look:

  module Enumerable
    # Take an associative array and unassociate it.
    #
    #   [[:a,1], [:b,2]].= unassociate.to_a     #=3D> [:a, [1], :b, [2]]
 =   #   [[:a,1], [:b,2]].unassociate(1).to_a  #=3D> [:a, 1= , :b, 2]
    #
    def unassociate(= index =3D 1..-1)
      return to_enum(:unassociate= , index) unless block_given?

      = each do |v|
        case v
  &= nbsp;     when Array
         = yield v[0]
          yield v[index]
        else
      &nbs= p;   yield v
          yield nil
        end
      end<= /div>
    end

  end
=

Is it a good method name? Is 1..-1 the best defaul= t?

Other thoughts/ suggestions?


------=_Part_276_7154744.1340580916814-- ------=_Part_275_27629376.1340580916814--