gpt4 book ai didi

Proc 的 Ruby 比较操作?

转载 作者:太空宇宙 更新时间:2023-11-03 16:22:41 27 4
gpt4 key购买 nike

比较运算符概述here (如下所示)在 ruby​​ 2.2.0 上实际上只是 Proc#call 结果与另一个对象的比较。文档内容如下:

proc === obj → result_of_proc

Invokes the blockwith obj as the proc's parameter like #call. It is to allow a procobject to be a target of when clause in a case statement.

我正在寻找的是:

a = Proc.new { puts 'hi' }
b = Proc.new { puts 'hi' }

a == b
# => true

这能以某种方式完成吗?

尝试解决 this 上的缓存失效问题要点(代码如下所示)。如果已经有一个缓存文件,那么对 Kinescope block 的任何更改都将被忽略。

module Kinescope
def self.film(reel,&block)
file_name = "kinescoped_#{reel}.ml"
begin
Marshal.load(File.read(file_name))
rescue
data = yield
File.open(file_name,'w') do |f|
f.write(Marshal.dump(data))
end
data
end
end
end

def time_consuming_calculation
sleep 60
true
end

test = Kinescope.film :big_data do
# 'film' gigantic data here
result = []
File.foreach('big_file.txt') do |line|
result << line if time_consuming_calculation
end
result
end

最佳答案

这称为函数问题,相当于解决停机问题。想想看。如果这是可能的,那么您可以像这样解决停机问题:

infinite_loop = -> { nil while true }
some_program == infinite_loop # => true

因此,不可能通过算法确定两个程序是否计算相同的函数。

关于Proc 的 Ruby 比较操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31224671/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com