【发布时间】:2020-03-06 17:31:34
【问题描述】:
我有一个字符串:
cipher = "0111101110010111001001010000000110101000001000111101110000110101100100001100101100000"
我想将它切片并存储在这样的数组中: ["01111011", "10010111" ... ]
我试过这段代码,但我有一个错误:
"cz.rb:16:in
<main>': undefined methodpush' for nil:NilClass (NoMethodError)"
i,j = 0,0
cipher_byte = []
while i < cipher.length
if i != 0 and i % 8 == 0
j+=1
end
cipher_byte[j].push(cipher[i])
p cipher_byte
i+=1
end
这有什么问题? 是红宝石。
【问题讨论】:
-
您的
cipher字符串包含 85 个字符。剩下的5个字符应该怎么处理? -
@KarolMazurek :检查
j的值。我敢打赌,在错误发生时它是cipher_byte数组之外的索引。
标签: arrays ruby string byte bit