我目前正在尝试分块读取二进制文件,到目前为止我的解决方案是这样的:
first_portion = File.binread(replay_file, 20)
second_portion = File.binread(replay_file, 24, 20)
Run Code Online (Sandbox Code Playgroud)
其中第一个数字是要读取的字节数,第二个数字是偏移量。
我知道这很糟糕,因为 File.binread 每次返回后都会关闭文件。我如何打开文件一次,执行我的操作,然后在完成后关闭它(但仍然使用 binread)。
另外,还有一个小问题。我一直在 python 中查看一些这样的示例,并看到了以下内容:
UINT32 = 'uintle:32'
length = binary_file.read(UINT32)
content = binary_file.read(8 * length)
Run Code Online (Sandbox Code Playgroud)
它到底在做什么(它是如何工作的),在 Ruby 中会是什么样子?
有没有办法在Ruby中有x个相同的参数?
最简单的问题是,你能缩短这个吗?
arr = [0,1,2,3]
if x == 1
return arr
elsif x == 2
return arr.product(arr)
elsif x == 3
return arr.product(arr, arr)
elsif x == 4
return arr.product(arr, arr, arr)
elsif x == 5
return arr.product(arr, arr, arr, arr)
end
Run Code Online (Sandbox Code Playgroud)