Search code examples
rubyintegerunsigned

How to declare 8-bit unsigned integer in ruby?


In c++ you can do:

uint8 foo_bar

How would we do the same thing in ruby? Any alternatives?

This post seems close to it maybe someone can explain?


Solution

  • Ruby abstracts away the internal storage of integers, so you don't have to worry about it.

    If you assign an integer to a variable, Ruby will deal with the internals, allocating memory when needed. Smaller integers are of type Fixnum (stored in a single word), larger integers are of type Bignum.

    a = 64
    a.class  #=> Fixnum; stored in a single word
    a += 1234567890
    a.class  #=> Bignum; stored in more than a single word
    

    Ruby is dynamically typed, so you cannot force a variable to contain only unsigned 8-bit integers (just as you cannot force a variable to only contain string values, etc.).