I want to use the combination method with a custom class.
If my class looks like this…
class MyClass
def initialize
@data = []
end
def to_a
@data
end
end
I could call this…
myobj = MyClass.new
myobj.to_a.combination(2) {|a,b| puts "#{a} #{b}" }
But I’d much rather have this…
myobj.combination {|a,b| puts "#{a} #{b}" }
I’ve tried to write a class method to wrap the combination method, passing the block. But it’s not working.
def combination(&block)
@data.to_a.combination(2) block.call
end
Also, does anyone know why combination is in the Array class and not Enumerable? I’d have thought it would have been more useful there.
The block is a special type of parameter to Array#combination (much like you’ve got it in your own definition). The correct invocation is: