- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在用我的家庭服务器尝试一些事情,并编写了一个小的 ruby 程序来按给定的数量填充 RAM。但实际上我必须将要放入 RAM 的字节数减半。我在这里遗漏了什么还是这是一个错误?
这里是代码:
class RAM
def initialize
@b = ''
end
def fill_ram(size)
puts 'Choose if you want to set the size in bytes, megabytes or gigabytes.'
answer = ''
valid = ['bytes', 'megabytes', 'gigabytes']
until valid.include?(answer)
answer = gets.chomp.downcase
if answer == 'bytes'
size = size * 0.5
elsif answer == 'megabytes'
size = size * 1024 * 1024 * 0.5
elsif answer == 'gigabytes'
size = size * 1024 * 1024 * 1024 * 0.5
else
puts 'Please choose between bytes, megabytes or gigabyte.'
end
end
size1 = size
if @b.bytesize != 0
size1 = size + @b.bytesize
end
until @b.bytesize == size1
@b << '0' * size
end
size = 0
end
def clear_ram
exit
end
def read_ram
puts 'At the moment this program fills ' + @b.bytesize.to_s + ' bytes of RAM'
end
end
想象一下,每一行的 "* 0.5"
都不会存在。
我确实在 IRB 中对其进行了测试,并且刚刚创建了一个新的 RAM 对象并用 1000 兆字节的数据填充了它。在我的例子中,它实际上用 2000 兆字节的数据填充了 RAM,所以我确实在每一行中添加了时间 0.5
,但这不是解决方案。
最佳答案
当我运行它时,我得到:
Choose if you want to set the size in bytes, megabytes or gigabytes.
bytes
At the moment this program fills 512 bytes of RAM
我认为问题是缺少编码检查。
我在 US-ASCII(一个字符 = 1 字节)中运行了我的测试。
如果您以 UTF-16 运行它,您就会对您的问题有一个解释。
你能试试下面的代码来检查你的编码吗:
p Encoding.default_internal
p Encoding.default_external
看完评论后:
脚本的结果取决于 RAM.fill_ram
的参数。您如何启动脚本 - 您多久调用一次 RAM.fill_ram
?
请提供完整代码。
我调用了我的例子
r = RAM.new
r.fill_ram(1024)
r.read_ram
关于Ruby 占用双倍的 RAM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23175532/
我有三个 double 组列表。我想将数组1复制到数组2并将其放入第三个数组中。 // ArrayList 1 [0.0, 1.0, 0.0, 1.0] // ArrayList 2 // Rando
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在编写一个带有变量 long balance 的银行程序将美分存入账户。当用户输入金额时,我有一种方法可以将美元转换为美分: public static long convertFromUsd (
如何 DRY(不要重复自己)RSpec 中的 double?例如: let(:s1) {instance_double(Class1, :att
我是 Java 新手,我想知道为什么当你有双 10/4 时却得到 2? double 是否总是必须有小数才能得到正确的答案?谢谢。 public class Super { public st
我正在用 Python 为 Fortran 库编写前端。 Python 模块应该可以在 32 位和 64 位机器上运行;适用于 Windows、Linux 和 Mac。 我想了解一些数据类型的字节宽度
我有两个ajax json。一个用于聊天用户列表列,另一个用于每个用户的消息历史记录。当单击 $('.member_list ul li') 元素时...第二个 ajax 将 li 的值作为 json
从 GHCi 中的 .hs 文件加载 a = 2+2.0 并执行 :t a 显示 a::Double。 另一方面,在 GHCi 中执行 let b = 2+2.0 和 :t b 会显示 b::Frac
我是一名优秀的程序员,十分优秀!