Well, your iteration will occur even when the definition has 0 instances and even with some instances will occur for definitions that have 0 used instances.
This iteration will skip definitions with no instances and no used instances:
def count_used_instances
model = Sketchup.active_model
puts "below are the definitions name and count"
definitions = model.definitions
those_used = []
puts "--------------------------------------------"
definitions.each { |definition|
next unless definition.instances.size > 0
num_used = definition.count_used_instances
next unless num_used > 0
puts "#{definition.name} : #{num_used}"
those_used << [ definition.name, num_used ]
}
# those_used is an array of nested [name, qty] arrays;
# return it as a hash with the name as the keys, qty as the value:
those_used.to_h
end