Method: LinuxStat::USB.count

Defined in:
lib/linux_stat/usb.rb

.countObject Also known as: count_devices

Opens /sys/bus/usb/devices, and counts the total number of devices connected to the USB interface. The return type is an integer.

It checks for devices with vendor and product id file. If there’s no such file, it will not count them as a USB device.

It could be also an integrated hub or a webcam, as well as external hotpluggable devices like printer, USB storage devices, USB mouse, USB keyboard, USB joypad etc.

But if the information isn’t available, it will return nil.



139
140
141
142
143
144
145
146
147
148
# File 'lib/linux_stat/usb.rb', line 139

def count
	@@sys_usb_readable ||= File.executable?('/sys/bus/usb/devices/')
	return nil unless @@sys_usb_readable

	Dir['/sys/bus/usb/devices/*/'.freeze].count { |x|
		id_vendor_file = File.join(x, 'idVendor'.freeze)
		id_product_file = File.join(x, 'idProduct'.freeze)
		File.readable?(id_vendor_file) && File.readable?(id_product_file)
	}
end