On Wed, Sep 2, 2009 at 1:41 AM, Max
Williams[email protected] wrote:
arr = (1…12).to_a
It feels like there should be a simple solution for this but i can’t
think of a nice way. Anyone?thanks
max
Thanks to a post by David M. in a recent thread, I learned about
Object#tap.
He used it on a hash.
I’ve used it here on an array.
Is there any problem using Object#tap in this way?
I saw some examples, but in the examples the object was not modified
in the block.
class Array
def spread(n)
dup.tap{|a| (size-n).downto(1).map{|b| size*b/(size-n+1)}.each{|c|
a.delete_at(c)}}
end
end
arr = (1…12).to_a
(3…12).each{|t| p arr.spread(t)}
###Output
[1, 6, 12]
[1, 4, 8, 12]
[1, 3, 6, 9, 12]
[1, 3, 5, 8, 10, 12]
[1, 2, 4, 6, 8, 10, 12]
[1, 2, 4, 6, 7, 9, 11, 12]
[1, 2, 3, 5, 6, 8, 9, 11, 12]
[1, 2, 3, 4, 6, 7, 8, 10, 11, 12]
[1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Harry