views:

35

answers:

1

I am working on a small script using the USB bindings for ruby.

Since this is just a wrapper around libusb, all of C++'s features are visible through the abstraction, for example when usb_bulk_read requires giving a buffer as an argument (which would just be a pointer in C).

How can I create a buffer which is 64bytes long, for the following bit of code, without having to create a string with 64 spaces in coded into the program (which I am doing ATM). I wish ruby had pointers for this!

data =  "          "; /#etc. You get the idea
handle.usb_bulk_read(1, data, 0)

Where handle is a USB::DevHandle object which has already been opened.

+1  A: 

Simple answer that probably avoids the core issue:

data = ' ' * 64
Matchu
That works, thanks. But if that is only the core issue, what are the others?
Ali Lown
@Ali But you are still using a string of 64 spaces, just as if you had hardcoded them all into the source. Nevertheless, this is probably the only way you are going to get the same effect in Ruby.
calmh
That is acceptable, since the device is coded to send in 64byte blocks, and anyway doing in c would have the same limitations.
Ali Lown