Comment by chao-
There is a bit of personal preference in what "applies to a number", but I see what you mean.
As a slight correction, a block is indeed an object! They are received by methods as an instance of the Proc class:
def inspects_block(&block)
puts block
puts block.class
end
inspects_block { "foo" }
# => #<Proc:0x0000000000000000>
# => Proc
You can even add a 'repeat' method to these in the way that you specified, although you will need to add '->' to declare the block (as a lambda, which is also just an instance of Proc) before you call #repeat on it: class Proc
def repeat(n)
n.times { self.call }
end
end
->{ puts("foo") }.repeat(3)
# => foo
# => foo
# => foo