Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Ruby中否定谓词过程_Ruby - Fatal编程技术网

在Ruby中否定谓词过程

在Ruby中否定谓词过程,ruby,Ruby,我有一个Proc,它是谓词 Proc.new { |number| number.even? } 有没有办法创建另一个具有相反含义的进程?我不能更改Proc的“body”,因为Proc将作为函数参数出现。所以我想要这样的东西: not(Proc.new { |number| number.even? } # which of course doesn't work :( 我希望它也能这样做 Proc.new { |number| number.odd? } 我想要一个类似的函数: def

我有一个Proc,它是谓词

Proc.new { |number| number.even? }
有没有办法创建另一个具有相反含义的进程?我不能更改Proc的“body”,因为Proc将作为函数参数出现。所以我想要这样的东西:

not(Proc.new { |number| number.even? }
# which of course doesn't work :(
我希望它也能这样做

Proc.new { |number| number.odd? }
我想要一个类似的函数:

def negate(proc)
  negated proc with meaning opposite of this of proc
end

提前非常感谢

以下方法返回与提供的过程相反的过程

def negate(procedure)
  Proc.new { |*args| !procedure.call(*args) }
end
或者,使用较短的符号:

def negate(procedure)
  proc { |*args| !procedure.call(*args) }
end
这有帮助吗

p = Proc.new { |number| number.even? }
p.call(1) #=> false
!p.call(1) #=> true

我不清楚。可能你是想否定这个过程的结果。对吗?是的,我认为它会起作用。请参阅一个工作示例(第22行)@ArupRakshit我不“更改”这里提供的过程的主体。我将其作为一个参数,并返回另一个以“相反”方式运行的过程。是的,它有帮助。:)非常感谢。