Ruby undefined方法each_slice错误

时间:2017-11-03 22:50:50

标签: ruby

我在GitHub上有一个有趣的应用程序。问题是我有一个错误,而且我根本不认识Ruby。

我收到错误消息:

/Library/Ruby/Gems/2.0.0/gems/dmm_util-0.1.0/lib/dmm_util/fluke28x_driver.rb:274:
in `parse_readings': undefined method `each_slice' for #<DmmUtil::ByteStr:0x007ffbf38e4620> (NoMethodError)

在代码中,有:

require 'rubygems'
require 'serialport'
require 'enumerator'

# ...

def parse_readings(reading_bytes)
  readings = {}
  ByteStr.new(reading_bytes).each_slice(30) do |reading_arr|
    r = reading_arr.map{|b| b.chr}.join
    # All bytes parsed
    readings[get_map_value(:readingid, r, 0)] = {
                           :value => get_double(r, 2),
                           :unit => get_map_value(:unit, r, 10),
                           :unit_multiplier => get_s16(r, 12),
                           :decimals => get_s16(r, 14),
                           :display_digits => get_s16(r, 16),
                           :state => get_map_value(:state, r, 18),
                           :attribute => get_map_value(:attribute, r, 20),
                           :ts => get_time(r, 22)
    }
  end
  readings
end

我的Ruby版本是:2.0.0p648

1 个答案:

答案 0 :(得分:1)

问题是你在一个未知类型的类上调用each_slice方法的类。 each_sliceenumerable方法,因此请尝试将其转换为数组对象。试一试:

     def parse_readings(reading_bytes)
      readings = {}
        bytestr = ByteStr.new(reading_bytes).chars.to_a 
         bytestr.each_slice(30) do|reading_arr|
            r = reading_arr.map{|b| b.chr}.join
            # All bytes parsed
            readings[get_map_value(:readingid, r, 0)] = {
                                   :value => get_double(r, 2),
                                   :unit => get_map_value(:unit, r, 10),
                                   :unit_multiplier => get_s16(r, 12),
                                   :decimals => get_s16(r, 14),
                                   :display_digits => get_s16(r, 16),
                                   :state => get_map_value(:state, r, 18),
                                   :attribute => get_map_value(:attribute, r, 20),
                                   :ts => get_time(r, 22)
            }
          end
          readings
      end