How each class splats

What's wrong with either:

arrayofA.each do |e|
  i,v = e.i,e.v
  p i,v
end

or

arrayofA.collect { |e| [e.i,e.v] }.flatten.each { |i,v| p i,v }

or

class << arrayofA
  def eachA
    each {|e| yield, e.i, e.v }
  end
end

if such a thing a "splat" were to be implemented, it would probably be
done by changing the "flatten" method to check whether the contents
respond to flatten instead of just recursively flattening arrays:

class Array
  def flatten
    a = []
    each do |x|
      if a.respond_to? :flatten
        x.flatten.each { |y| a << y }
      else
        a << x
      end
    end
    a
  end
end

···

-----Original Message-----
From: Trans [mailto:transfire@gmail.com]
Sent: Monday, 26 September 2005 2:42 PM
To: ruby-talk ML
Subject: How each class splats

First I'll just ask it there's a way to do this WITHOUT redefining #each
in Array.

  class A
    def initialize(i,v)
      @i,@v=i,v
    end
  end

  arrayofA = [ A.new(:a,1), A.new(:b,2) ]

  arrayofA.each { |e| p e }
  => #<A @i=:a,@v=1>
     #<A @i=:b,@v=2>

  arrayofA.each { |i,v| p i,v }
  => :a
     1
     :b
     2

If thsi is not possible, what do people think of allowing a class to
define a #splat method to determine how it will splat into an each block
(and perhaps other things).

  class A
    def initialize(i,v)
      @i,@v=i,v
    end
    def splat( arity )
      case arity
      when 1 then self
      when 2 then [@i,@v]
      else
        [@i,@v].concat( [nil]*(arity-2) )
      end
    end
  end

Or something to that effect.

Thanks,
T.

#####################################################################################
This email has been scanned by MailMarshal, an email content filter.
#####################################################################################

arrayofA.collect { |e| [e.i,e.v] }.flatten.each { |i,v| p i,v }

Perhaps you mean

arrayofA.collect { |e| [e.i,e.v] }.each { |(i,v)| p i,v }

Kevin Ballard wrote:

arrayofA.collect { |e| [e.i,e.v] }.flatten.each { |i,v| p i,v }

Perhaps you mean

arrayofA.collect { |e| [e.i,e.v] }.each { |(i,v)| p i,v }

No brackets needed

arrayOfA.map {|e| [e.i,e.v]}.each {|i,v| puts i,v}

a
1
b
2
=> [[:a, 1], [:b, 2]]

Kind regards

    robert