How to declare 8-bit unsigned integer in ruby?

旧街凉风 提交于 2019-11-28 06:15:50

问题


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?


回答1:


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.).




回答2:


You don't declare types in Ruby. The language is dynamically typed.



来源:https://stackoverflow.com/questions/1335710/how-to-declare-8-bit-unsigned-integer-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!