I wanted to find out the memory consumed (in bytes) by data types. I called size method on an integer. Since I am running a 64 bit machine, it returned 8.
1.size # => 8
Similarly, for strings and arrays, it returned 1 byte per character/integer.
'a'.size # => 1
['a'].size # => 1
['a', 1].size # => 2
- Why is there no size method for float?
- Shouldn't heterogeneous arrays like
['a', 1]return1 + 8 = 9 bytes(1 for char, 8 for integer)? - Is it correct to call
sizeto check memory allocated to ruby data types?
size. In the text, you indicate that float does not have it. It is not clear what you mean.'€π'.sizereturns2; so.sizecounts the number of characters in strings, not bytes.