Probably slightly faster to do map! instead of map

unpacked = value.unpack("C*")
unpacked.map { |b| "\\x%02x" % b }
io.write("\"#{unpacked.join(nil)\"")

Also if this is super common, it might be worth #defining (so I guess a constant?) the "\\x%02x"