未定义方法`shift'for“[97,98,257,256]”

时间:2017-03-13 20:22:24

标签: ruby

def uncompress(to_uncompress)
  dictionary = (0..255).to_a.map { |element| element.chr }
  output = Array.new
  current = to_uncompress.shift
  output << current
  to_uncompress.each_char do |index|
    previous = current
    current = index
    if current < dictionary.length
      s = dictionary[current]
      output << s
      dictionary << dictionary[previous] + s[0]
    else
      s = dictionary[previous]
      output << s
      dictionary << s
    end
  end
  output.shift
  output
end

[97,98,257,256]是压缩数组,当我调用uncompress方法时,它会抛出未定义的方法`shift'为“[97,98,257,256]”:String(NoMethodError)

1 个答案:

答案 0 :(得分:2)

如果你给它一个字符串文字,它可能是JSON数据,或者至少它是JSON解析器可以处理的东西:

require 'json'

现在它是一个合适的阵列。您可能需要在文件顶部 Sub RowMatch() Dim x As Integer ' Make sure we are in the right sheet Worksheets("Q416").Activate ' Set numrows = number of rows of data. NumRows = Range("C2", Range("C2").End(xlDown)).Rows.count ' find the reference range Worksheets("Q415").Activate NumRows2 = Range("C5", Range("C5").End(xlDown)).Rows.count Worksheets("Q416").Activate MsgBox ("Total # of Rows on this sheet = " & NumRows & " and " & NumRows2 & " in Ref Range") Range("A1").Select ' Establish "For" loop to loop "numrows" number of times. For x = 1 To NumRows 'MsgBox NumRows2 For y = 1 To NumRows2 'MsgBox (ActiveCell.Offset(x, 0).Value & " & " & Worksheets("Q415").Cells(y + 1, 1)) If ActiveCell.Offset(x, 0).Value = Worksheets("Q415").Cells(y + 1, 1).Value _ And ActiveCell.Offset(x, 2).Value = Worksheets("Q415").Cells(y + 1, 3).Value Then If ActiveCell.Offset(x, 5).Value = Worksheets("Q415").Cells(y + 1, 6).Value Then 'If NumRows(i).Value = ActiveCell.Offset(1, 0).Value Then ActiveCell.Offset(x, 10).Value = "Same" Else ActiveCell.Offset(x, 10).Value = ActiveCell.Offset(x, 5).Value - Worksheets("Q415").Cells(y + 1, 6).Value End If End If Next y Next x End Sub 来加载JSON模块。