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?
These are two different methods that serves different purpose for two different data types.
In eg 1, you are applying
sizeto fixnum. This method:source: http://www.ruby-doc.org/core-2.2.0/Fixnum.html#method-i-size
However when used with array,
sizeis alias forlength. Here: http://www.ruby-doc.org/core-2.1.2/Array.html#method-i-size. Which: