Q:

ruby-snmp: how to automatically convert response to its proper type?

In ruby I read some SNMP registers. Response is an array of objects.
Is there a nice way to convert each object to the proper type avoiding the case..when in the following code? It looks strange that it must be converted  manually as the type is already known:
require 'snmp'

HOST = '127.0.0.1'.freeze

registers = ['sysContact.0', 'sysUpTime.0',
             'upsIdentManufacturer.0', 'upsIdentModel.0', 'upsIdentName.0']

params_array = {}
SNMP::Manager.open(host: HOST) do |manager|
  manager.load_module('UPS-MIB')
  response = manager.get(registers)
  response.each_varbind do |vb|

    ##################################
    # change from here...

    value = nil
    case vb.value.asn1_type
    when 'OCTET STRING'        # <==========
      value = vb.value        
    when 'INTEGER'             # <==========
      value = vb.value.to_i
    when 'TimeTicks'           # <==========
      value = vb.value.to_s
    else
      puts "Type '#{vb.value.asn1_type}' not recognized!"
      exit(1)
    end
    params_array[vb.name.to_s] = value

    # ... to here
    ##################################

    # with something like
    # params_array[vb.name.to_s] = vb.value._to_its_proper_type_

  end
end
pp params_array

A:

Looking at the code in the gem repo, it doesn't look like like there is a method for this.  I suppose you could try to monkey patch it, but not sure if it's worth the trouble.
If you don't like the switch syntax, you could just use a hash lookup like this:
require 'snmp'

HOST = '127.0.0.1'.freeze

TYPE_VALUES = {
  'OCTET STRING' => :to_s,
  'INTEGER' => :to_i,
  'TimeTicks' => :to_s
}.freeze

registers = ['sysContact.0', 'sysUpTime.0',
             'upsIdentManufacturer.0', 'upsIdentModel.0', 'upsIdentName.0']

params_array = {}

SNMP::Manager.open(host: HOST) do |manager|
  manager.load_module('UPS-MIB')
  response = manager.get(registers)
  response.each_varbind do |vb|
    if method = TYPE_VALUES[vb.value.ans1_type]
      params_array[vb.name.to_s] = vb.value.send(method)
    else
      puts "Type '#{vb.value.asn1_type}' not recognized!"
      exit(1)
    end
  end
end
pp params_array

