user7148991
user7148991

Reputation:

Ruby undefined method each_slice error

I've an interesting app on GitHub. The problem is that I have an error, and that I don't know Ruby at all.

I have the error message:

/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)

In the code, there is:

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

My Ruby version is: 2.0.0p648

Upvotes: 1

Views: 1304

Answers (1)

MisterCal
MisterCal

Reputation: 622

The problem is the class you're invoking the each_slice method on a class of an unknown type. each_slice is an enumerable method, so try converting it to an array object. Give this a try:

     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

Upvotes: 1

Related Questions