Search code examples
rubyusblibusb

LibUSB uninitialized constant USB (NameError)


I tried to run some code with libusb:

require 'libusb'
USB::devices.each { |d| puts "device (#{d.product_name}) vendor: #{d.vendor_id} product: #{d.product_id}" }

But when i run it:

C:\icd_programm>ruby libusb.rb
libusb.rb:2:in `<main>': uninitialized constant USB (NameError)

Why do i get such an error? I followed this instructions:http://www.technofetish.net/mike/demo1.txt


Solution

  • The error means that there is no class or module named USB. The instructions you referred to seem to use an older version of libusb that originally defined the USB module.

    Here's how to do it in newer versions of libusb:

    require 'libusb'
    
    usb = LIBUSB::Context.new
    usb.devices.each{ ... }
    

    Source: https://github.com/larskanis/libusb