Reputation: 61
Mysql2 reads in bit columns as strings by default.
My schema:
In Ruby:
How do I read them as bytes?
Upvotes: 0
Views: 155
Reputation: 23939
There may be a better way to deal w/it in the Mysql library, but in my experience, strings are how Ruby deals with binary data. The binary file reader methods do the same thing.
Check out the #unpack
method. You can do stuff like:
[16] pry(main)> bytes = "\x00\x01\x02"
=> "\u0000\u0001\u0002"
[17] pry(main)> bytes.unpack('C*')
=> [0, 1, 2]
Upvotes: 1
Reputation: 61
I'd have expected Mysql2 to cast the bit into bytes by default. But it doesn't do that as of now (I'm using 0.3.16). What I ended up doing was to covert it into bytes myself:
row[:can_sell].bytes => [0]
Upvotes: 0