I have a Proc, which is predicate.
Proc.new { |number| number.even? }
Is there a way to somehow create another Proc which have the opposite meaning? I can't change the "body" of the Proc, because the Proc will come as function parameter. So I want something like this:
not(Proc.new { |number| number.even? }
# which of course doesn't work :(
and I want it to do the same as
Proc.new { |number| number.odd? }
The idea is that I want a function similar to this:
def negate(proc)
negated proc with meaning opposite of this of proc
end
Thank you very much in advance!
The following method returns the procedure opposite to the provided one.
def negate(procedure)
Proc.new { |*args| !procedure.call(*args) }
end
or, using shorter notation:
def negate(procedure)
proc { |*args| !procedure.call(*args) }
end
Does this help at all?
p = Proc.new { |number| number.even? }
p.call(1) #=> false
!p.call(1) #=> true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With