T
Trans
A question and a thought (or two).
Is there a way to return from a block without returning from the whole
method? Eg.
# This behavior
def double_collect( array )
array.collect { |v| v*2 }
end
# Nope, returns from double_collect
def double_collect( array )
array.collect { |v| return v*2 }
end
# Nope, returns only first iteration
def double_collect( array )
array.collect { |v| break v*2 }
end
Would something like #local_return be useful?
The reason I ask is b/c I was also thinking about a possbile special
form of 'return' that can handle hash assocations:
def double_collect( array )
array.collect { |v| local_return v => v*2 }
end
double_collect( [1,2,3] ) #=> { 1=>2, 2=>4, 3=>6 }
Presently I use a facet called #graph to do this:
array.graph { |v| [v, v*2] }
But returning that array just feels wrong (not to mention inefficient).
Thanks,
T.
Is there a way to return from a block without returning from the whole
method? Eg.
# This behavior
def double_collect( array )
array.collect { |v| v*2 }
end
# Nope, returns from double_collect
def double_collect( array )
array.collect { |v| return v*2 }
end
# Nope, returns only first iteration
def double_collect( array )
array.collect { |v| break v*2 }
end
Would something like #local_return be useful?
The reason I ask is b/c I was also thinking about a possbile special
form of 'return' that can handle hash assocations:
def double_collect( array )
array.collect { |v| local_return v => v*2 }
end
double_collect( [1,2,3] ) #=> { 1=>2, 2=>4, 3=>6 }
Presently I use a facet called #graph to do this:
array.graph { |v| [v, v*2] }
But returning that array just feels wrong (not to mention inefficient).
Thanks,
T.