T
Trans
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.newa,1), A.newb,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.
#each in Array.
class A
def initialize(i,v)
@i,@v=i,v
end
end
arrayofA = [ A.newa,1), A.newb,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.